flgen 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0c98696eca3ffddc4f49695ec9fe26ad40bebaa8b4ef432a9a24455059ec675f
4
+ data.tar.gz: 2d4cacf96065ceeebf2c5a662fd0d2f2706e9fb97363a3990eec9e77d06d1ad8
5
+ SHA512:
6
+ metadata.gz: '065852d3c973c0a108e1fccaea7cf7219e9c734fe9f75adff152ddd05df406fe8cb3d92a1a6281bf5494b22d279c60b7d69ba2eec0a104fcfe6a6badbda1dcfe'
7
+ data.tar.gz: 70eb64d338dad8169c3a577228b4640fa91c4e5645aa8432401996aa95696a53cca8d5d0e2d1b01568e0a7082b1a00fd0692a4650c508a9945cf56e420d1429a
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,146 @@
1
+ [![CI](https://github.com/pezy-computing/flgen/actions/workflows/ci.yml/badge.svg)](https://github.com/pezy-computing/flgen/actions/workflows/ci.yml)
2
+
3
+ # FLGen
4
+
5
+ ファイルリストを記述するための DSL と、ファイルリストを生成するための実行コマンドを提供します。
6
+
7
+ ## インストール
8
+
9
+ ### Ruby
10
+
11
+ FLGen は [Ruby](https://www.ruby-lang.org) で実装されているので、実行には Ruby のインストールが必要です。
12
+ サポートする Ruby のバージョンは 3.0 以上です。インストール方法については、[こちら](https://www.ruby-lang.org/en/downloads/)を参照ください。
13
+
14
+ ### インストールコマンド
15
+
16
+ FLGen をインストールするには、以下のコマンドを実行します。
17
+
18
+ ```
19
+ $ gem install flgen
20
+ ```
21
+
22
+ ## DSL
23
+
24
+ ファイルリストを記述するための DSL として以下の構文が定義されています。
25
+
26
+ * `source_file(path, from: :current)`
27
+ * 指定したファイルをファイルリストに追加します
28
+ * `file_list(path, from: :root)`
29
+ * 指定したファイルリストを読み込みます
30
+ * `define_macro(name, value = nil)`
31
+ * マクロを定義します
32
+ * `macro_defined?(name)`
33
+ * 指定したマクロが定義されているかどうかを返します
34
+ * `include_directory(path, from: :current)`
35
+ * 指定したパスをインクルードパスとして追加します
36
+ * `target_tool?(tool)`
37
+ * `tool` が対象ツールかどうかを返します
38
+ * `compile_argument(argument, tool: nil)`
39
+ * コンパイル引数を追加します
40
+ * `tool` が指定されている場合は、対象ツールの場合にのみ、引数を追加します
41
+ * `runtime_argument(argument, tool: nil)`
42
+ * 実行時引数を追加します
43
+ * `tool` が指定されている場合は、対象ツールの場合にのみ、引数を追加します
44
+
45
+ Ruby の言語内 DSL として実装されているので、以下の様に `if` など Ruby の構文も使用することができます。
46
+
47
+ ```ruby
48
+ if target_tool? :vcs
49
+ compile_argument '-sverilog'
50
+ end
51
+
52
+ 10.times do |i|
53
+ source_file "foo_#{i}.sv"
54
+ end
55
+ ```
56
+
57
+ ### `from` オプション引数について
58
+
59
+ `from` は指定されたファイルやディレクトリの基準ディレクトリを指定する引数で、`:current`/`root`/`local_root` を指定することができます。
60
+
61
+ * `:current`
62
+ * 現在のファイルリストがある場所を基準とします
63
+ * `:root`
64
+ * `.git` があるリポジトリのルートディレクトリを基準ディレクトリとします
65
+ * あるリポジトリのサブモジュール (上位階層にも `.git` がある) 場合、上位リポジトリのルートディレクトリから順に検索を行います
66
+ * `:local_root`
67
+ * 自身が含まれるリポジトリのルートディレクトリを基準ディレクトリとします
68
+
69
+ ただし、与えられたファイルやディレクトリが絶対パスで指定されている場合は、`from` に関係なく、そのまま追加されます。
70
+
71
+ #### 例
72
+
73
+ 以下のディレクトリ構造になっていたとします。
74
+
75
+ ```
76
+ foo_project
77
+ +-- .git
78
+ +-- bar_project
79
+ | +-- .git
80
+ | +-- common
81
+ | | `-- common.sv
82
+ | `-- src
83
+ | + bar.list.rb
84
+ | ` bar.sv
85
+ `-- common
86
+ `-- common.sv
87
+ ```
88
+
89
+ * `bar.list.rb` で `source_file 'bar.sv', from: :current` とある場合
90
+ * `foo_project/bar_project/src/bar.sv` が追加される
91
+ * `bar.list.rb` で `source_file 'common/common.sv', from: :root` とある場合
92
+ * `foo_project/common/common.sv` が追加される
93
+ * `bar.list.rb` で `source_file 'common/common.sv', from: :local_root` とある場合
94
+ * `foo_project/bar_project/common/common.sv` が追加される
95
+
96
+ ## 実行コマンド
97
+
98
+ `flgen` が実行コマンドです。`flgen` に DSL で記述されたファイルリストを与えると、EDA ツールに与えるためのファイルリストを出力します。
99
+ また、以下のオプションがあります。
100
+
101
+ * `--define-macro=MACRO[,MACRO]`
102
+ * マクロ定義を追加します
103
+ * `--include-directory=DIR[,DIR]`
104
+ * インクルードディレクトリを追加します
105
+ * `--compile`
106
+ * 出力されるファイルリストは `runtime_argument` で指定された実行時引数を含みません
107
+ * `--runtime`
108
+ * 出力されるファイルリストは `runtime_argument` で指定された実行時引数だけを含みます
109
+ * `--tool=TOOL`
110
+ * 対象となる EDA ツールを指定します
111
+ * `compile_argument`/`runtime_argument` でツールの指定がある場合、一致する引数がファイルリストに出力されます
112
+ * `--rm-ext=EXT[,EXT]`
113
+ * 指定された拡張子をソースファイルから削除します
114
+ * `--collect-ext=EXT[,EXT]`
115
+ * 指定された拡張子を持つソースファイルのみがファイルリストに出力されます
116
+ * `--output=FILE`
117
+ * 出力するファイルリストのファイル名を指定します
118
+ * 指定がない場合は、標準出力に出力されます
119
+ * `--[no-]print-header`
120
+ * ヘッダーを出力するかどうかを指定します。
121
+ * `--source-file-only`
122
+ * ソースファイルのみがファイルリストに出力されます
123
+
124
+ ## サンプル
125
+
126
+ https://github.com/pezy-computing/flgen/tree/master/sample
127
+
128
+ にサンプルがあります。
129
+
130
+ ## ライセンス
131
+
132
+ Apache-2.0 ライセンスの元で公開しています。詳しくは、下記及び [LICENSE](LICENSE) を参照ください。
133
+
134
+ Copyright 2022 PEZY Computing K.K.
135
+
136
+ Licensed under the Apache License, Version 2.0 (the "License");
137
+ you may not use this file except in compliance with the License.
138
+ You may obtain a copy of the License at
139
+
140
+ http://www.apache.org/licenses/LICENSE-2.0
141
+
142
+ Unless required by applicable law or agreed to in writing, software
143
+ distributed under the License is distributed on an "AS IS" BASIS,
144
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145
+ See the License for the specific language governing permissions and
146
+ limitations under the License.
data/exe/flgen ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
5
+ require 'flgen'
6
+
7
+ begin
8
+ FLGen::CLI.new.run(ARGV)
9
+ rescue FLGen::FLGenError => e
10
+ abort e.to_s
11
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ module Arguments
5
+ class Base
6
+ def initialize(type, tool)
7
+ @type = type
8
+ @tool = tool
9
+ end
10
+
11
+ attr_reader :type
12
+ attr_reader :tool
13
+
14
+ def match_tool?(target_tool)
15
+ tool.nil? || target_tool && (tool == target_tool) || false
16
+ end
17
+ end
18
+
19
+ class Define < Base
20
+ def initialize(name, value)
21
+ super(:define, nil)
22
+ @name = name
23
+ @value = value
24
+ end
25
+
26
+ attr_reader :name
27
+ attr_reader :value
28
+ end
29
+
30
+ class Include < Base
31
+ def initialize(path)
32
+ super(:include, nil)
33
+ @path = path
34
+ end
35
+
36
+ attr_reader :path
37
+ end
38
+
39
+ class Generic < Base
40
+ def initialize(argument, tool)
41
+ super(:generic, tool)
42
+ @argument = argument
43
+ end
44
+
45
+ attr_reader :argument
46
+ end
47
+ end
48
+ end
data/lib/flgen/cli.rb ADDED
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ class CLI
5
+ PROGRAM_NAME = 'flgen'
6
+
7
+ def run(args)
8
+ options, remain_args = parse_options(args.dup)
9
+ context = parse_file_list(options, remain_args)
10
+ output_result(args, context)
11
+ end
12
+
13
+ private
14
+
15
+ def parse_options(args)
16
+ options = {
17
+ macros: [], include_directories: [], runtime: false, tool: nil,
18
+ rm_ext: [], collect_ext: [], format: :filelist, print_header: true,
19
+ source_file_only: false
20
+ }
21
+ option_parser(options).parse!(args)
22
+ [options, args]
23
+ end
24
+
25
+ def option_parser(options)
26
+ OptionParser.new do |parser|
27
+ parser.version = VERSION
28
+ parser.program_name = PROGRAM_NAME
29
+
30
+ parser.on('--define-macro=MACRO[,MACRO]', Array) do |macros|
31
+ options[:macros].concat(macros)
32
+ end
33
+ parser.on('--include-directory=DIR[,DIR]', Array) do |directories|
34
+ options[:include_directories].concat(directories)
35
+ end
36
+ parser.on('--compile') do
37
+ options[:runtime] = false
38
+ end
39
+ parser.on('--runtime') do
40
+ options[:runtime] = true
41
+ end
42
+ parser.on('--tool=TOOL') do |tool|
43
+ options[:tool] = tool.to_sym
44
+ end
45
+ parser.on('--rm-ext=EXT[,EXT]', Array) do |ext|
46
+ options[:rm_ext].concat(ext)
47
+ end
48
+ parser.on('--collect-ext=EXT[,EXT]', Array) do |ext|
49
+ options[:collect_ext].concat(ext)
50
+ end
51
+ parser.on('--format=FORMAT') do |format|
52
+ options[:format] = format.to_sym
53
+ end
54
+ parser.on('--output=FILE') do |file|
55
+ options[:output] = file
56
+ end
57
+ parser.on('--[no-]print-header') do |value|
58
+ options[:print_header] = value
59
+ end
60
+ parser.on('--source-file-only') do |value|
61
+ options[:source_file_only] = value
62
+ end
63
+ end
64
+ end
65
+
66
+ def parse_file_list(options, args)
67
+ context = create_context(options)
68
+ top_level = FileList.new(context, '')
69
+ args.each { |arg| load_file_list(top_level, arg) }
70
+ context
71
+ end
72
+
73
+ def create_context(options)
74
+ context = Context.new(options)
75
+ context
76
+ .options[:macros]
77
+ .each(&context.method(:define_macro))
78
+ context
79
+ .options[:include_directories]
80
+ .each(&context.method(:add_include_directory))
81
+ context
82
+ end
83
+
84
+ def load_file_list(top_level, arg)
85
+ path = File.expand_path(arg)
86
+ top_level.file_list(path)
87
+ end
88
+
89
+ def output_result(args, context)
90
+ formatter = create_formatter(context)
91
+ print_header(context, formatter, args)
92
+ if context.options[:output]
93
+ File.open(context.options[:output], 'w') { |io| formatter.output(io) }
94
+ else
95
+ formatter.output($stdout)
96
+ end
97
+ end
98
+
99
+ def create_formatter(context)
100
+ formatter = Formatter.formatters[context.options[:format]]
101
+ formatter.new(context)
102
+ end
103
+
104
+ def print_header(_context, formatter, args)
105
+ formatter.header_lines << "#{PROGRAM_NAME} version #{FLGen::VERSION}"
106
+ formatter.header_lines << 'applied arguments'
107
+ args.each do |arg|
108
+ formatter.header_lines << " #{arg}"
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ class Context
5
+ def initialize(options)
6
+ @options = options
7
+ end
8
+
9
+ attr_reader :options
10
+
11
+ def source_files
12
+ @source_files ||= []
13
+ end
14
+
15
+ def add_source_file(root, path)
16
+ return if runtime?
17
+
18
+ file = SourceFile.new(root, path)
19
+ add_source_file?(file) &&
20
+ (source_files << file.remove_ext(@options[:rm_ext]))
21
+ end
22
+
23
+ def define_macro(macro, value = nil)
24
+ k, v =
25
+ if value.nil? && macro.respond_to?(:split)
26
+ macro.split('=', 2)
27
+ else
28
+ [macro, value]
29
+ end
30
+ add_macro_definition(k.to_sym, v)
31
+ end
32
+
33
+ def macros
34
+ @macros ||= []
35
+ end
36
+
37
+ def add_include_directory(directory)
38
+ return if include_directory_already_added?(directory)
39
+
40
+ add_compile_argument(Arguments::Include.new(directory))
41
+ end
42
+
43
+ def add_compile_argument(argument)
44
+ return if runtime?
45
+
46
+ add_argument(argument)
47
+ end
48
+
49
+ def add_runtime_argument(argument)
50
+ return unless runtime?
51
+
52
+ add_argument(argument)
53
+ end
54
+
55
+ def arguments
56
+ @arguments ||= []
57
+ end
58
+
59
+ def loaded_file_lists
60
+ @loaded_file_lists ||= []
61
+ end
62
+
63
+ private
64
+
65
+ def runtime?
66
+ options[:runtime]
67
+ end
68
+
69
+ def add_source_file?(file)
70
+ target_ext?(file) && !source_file_already_added?(file)
71
+ end
72
+
73
+ def target_ext?(file)
74
+ return true unless @options.key?(:collect_ext)
75
+ return true if @options[:collect_ext].empty?
76
+
77
+ file.match_ext?(@options[:collect_ext])
78
+ end
79
+
80
+ def source_file_already_added?(file)
81
+ checksum = file.checksum
82
+ path = file.path
83
+
84
+ return true if checksums[path].include?(checksum)
85
+
86
+ checksums[path] << checksum
87
+ false
88
+ end
89
+
90
+ def checksums
91
+ @checksums ||= Hash.new { |h, k| h[k] = [] }
92
+ end
93
+
94
+ def add_macro_definition(name, value)
95
+ macros << name unless macros.include?(name)
96
+ arguments
97
+ .delete_if { |argument| argument.type == :define && argument.name == name }
98
+ add_compile_argument(Arguments::Define.new(name, value))
99
+ end
100
+
101
+ def include_directory_already_added?(path)
102
+ arguments
103
+ .any? { |argument| argument.type == :include && argument.path == path }
104
+ end
105
+
106
+ def add_argument(argument)
107
+ return unless argument.match_tool?(options[:tool])
108
+
109
+ arguments << argument
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ class FLGenError < StandardError
5
+ end
6
+
7
+ class NoEntryError < FLGenError
8
+ def initialize(path, location)
9
+ message =
10
+ "no such file or directory -- #{path} " \
11
+ "@#{location.path}:#{location.lineno}"
12
+ super(message)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ class FileList
5
+ def initialize(context, path)
6
+ @context = context
7
+ @path = path
8
+ @root_directories = extract_root
9
+ end
10
+
11
+ def file_list(path, from: :root, raise_error: true)
12
+ root = find_root(path, from, :file?)
13
+ load_file_list(root, path, raise_error)
14
+ end
15
+
16
+ def source_file(path, from: :current, raise_error: true)
17
+ root = find_root(path, from, :file?)
18
+ add_source_file(root, path, raise_error)
19
+ end
20
+
21
+ def define_macro(macro, value = nil)
22
+ @context.define_macro(macro, value)
23
+ end
24
+
25
+ def macro_defined?(macro)
26
+ @context.macros.include?(macro.to_sym)
27
+ end
28
+
29
+ def include_directory(path, from: :current, raise_error: true)
30
+ root = find_root(path, from, :directory?)
31
+ add_include_directory(root, path, raise_error)
32
+ end
33
+
34
+ def target_tool?(tool)
35
+ target_tool = @context.options[:tool]
36
+ target_tool && tool.to_sym == target_tool || false
37
+ end
38
+
39
+ def compile_argument(argument, tool: nil)
40
+ @context.add_compile_argument(Arguments::Generic.new(argument, tool))
41
+ end
42
+
43
+ def runtime_argument(argument, tool: nil)
44
+ @context.add_runtime_argument(Arguments::Generic.new(argument, tool))
45
+ end
46
+
47
+ private
48
+
49
+ def extract_root
50
+ return nil if @path.empty?
51
+
52
+ Pathname
53
+ .new(@path)
54
+ .dirname
55
+ .descend
56
+ .select(&method(:repository_root?))
57
+ .map(&:to_s)
58
+ end
59
+
60
+ def repository_root?(path)
61
+ File.exist?(path.join('.git').to_s)
62
+ end
63
+
64
+ def find_root(path, from, checker)
65
+ if absolute_path?(path)
66
+ ''
67
+ elsif from == :current
68
+ current_directory
69
+ else
70
+ lookup_root(path, from, checker)
71
+ end
72
+ end
73
+
74
+ def absolute_path?(path)
75
+ Pathname.new(path).absolute?
76
+ end
77
+
78
+ def current_directory
79
+ # From Ruby 3.1 Thread::Backtrace::Location#absolute_path returns nil
80
+ # for code string evaluated by eval methods
81
+ # see https://github.com/ruby/ruby/commit/64ac984129a7a4645efe5ac57c168ef880b479b2
82
+ location = caller_locations(3, 1).first
83
+ path = location.absolute_path || location.path
84
+ File.dirname(path)
85
+ end
86
+
87
+ def lookup_root(path, from, checker)
88
+ (from == :root && @root_directories || [@root_directories.last])
89
+ .find { |root| File.__send__(checker, File.join(root, path)) }
90
+ end
91
+
92
+ def load_file_list(root, path, raise_error)
93
+ entry_exist?(root, path, :file?, raise_error) || return
94
+
95
+ # Need to File.realpath to resolve symblic link
96
+ list_path = File.realpath(concat_path(root, path))
97
+ file_list_already_loaded?(list_path) && return
98
+
99
+ @context.loaded_file_lists << list_path
100
+ self.class.new(@context, list_path)
101
+ .instance_eval(File.read(list_path), list_path)
102
+ end
103
+
104
+ def file_list_already_loaded?(path)
105
+ @context.loaded_file_lists.include?(path)
106
+ end
107
+
108
+ def add_source_file(root, path, raise_error)
109
+ entry_exist?(root, path, :file?, raise_error) || return
110
+ @context.add_source_file(root, path)
111
+ end
112
+
113
+ def add_include_directory(root, path, raise_error)
114
+ entry_exist?(root, path, :directory?, raise_error) || return
115
+
116
+ directory_path = concat_path(root, path)
117
+ @context.add_include_directory(directory_path)
118
+ end
119
+
120
+ def entry_exist?(root, path, checker, raise_error)
121
+ result = root && File.__send__(checker, concat_path(root, path)) || false
122
+ result ||
123
+ raise_error && (raise NoEntryError.new(path, caller_locations(3, 1)[0]))
124
+ end
125
+
126
+ def concat_path(root, path)
127
+ File.expand_path(path, root)
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ class FileListFormatter < Formatter
5
+ def format_header_line(line)
6
+ "// #{line}"
7
+ end
8
+
9
+ def format_macro(macro, value)
10
+ if value.nil?
11
+ "+define+#{macro}"
12
+ else
13
+ "+define+#{macro}=#{value}"
14
+ end
15
+ end
16
+
17
+ def format_include_directory(directory)
18
+ "+incdir+#{directory}"
19
+ end
20
+
21
+ def fomrat_argument(argument)
22
+ argument
23
+ end
24
+
25
+ def format_file_path(path)
26
+ path
27
+ end
28
+
29
+ Formatter.add_formatter(:filelist, self)
30
+ end
31
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ class Formatter
5
+ class << self
6
+ def add_formatter(type, formatter)
7
+ formatters[type] = formatter
8
+ end
9
+
10
+ def formatters
11
+ @formatters ||= {}
12
+ end
13
+ end
14
+
15
+ def initialize(context)
16
+ @context = context
17
+ @header_lines = []
18
+ end
19
+
20
+ attr_reader :header_lines
21
+
22
+ def output(io)
23
+ print_header(io)
24
+ print_macros(io)
25
+ print_include_directoris(io)
26
+ print_arguments(io)
27
+ print_source_files(io)
28
+ end
29
+
30
+ private
31
+
32
+ def print_header(io)
33
+ return unless print_header?
34
+
35
+ header_lines.each do |line|
36
+ io.puts(format_header_line(line))
37
+ end
38
+ end
39
+
40
+ def print_header?
41
+ @context.options[:output] &&
42
+ @context.options[:print_header] && !@context.options[:source_file_only]
43
+ end
44
+
45
+ def each_argument(type, &block)
46
+ @context.arguments.each do |argument|
47
+ argument.type == type && block.call(argument)
48
+ end
49
+ end
50
+
51
+ def print_macros(io)
52
+ return if source_file_only?
53
+
54
+ pre_macros(io)
55
+ each_argument(:define) do |argument|
56
+ io.puts(format_macro(argument.name, argument.value))
57
+ end
58
+ post_macros(io)
59
+ end
60
+
61
+ def pre_macros(_io)
62
+ end
63
+
64
+ def post_macros(_io)
65
+ end
66
+
67
+ def print_include_directoris(io)
68
+ return if source_file_only?
69
+
70
+ pre_include_directories(io)
71
+ each_argument(:include) do |argument|
72
+ io.puts(format_include_directory(argument.path))
73
+ end
74
+ post_include_directories(io)
75
+ end
76
+
77
+ def pre_include_directories(_io)
78
+ end
79
+
80
+ def post_include_directories(_io)
81
+ end
82
+
83
+ def print_arguments(io)
84
+ return if source_file_only?
85
+
86
+ pre_arguments(io)
87
+ each_argument(:generic) do |argument|
88
+ io.puts(fomrat_argument(argument.argument))
89
+ end
90
+ post_arguments(io)
91
+ end
92
+
93
+ def pre_arguments(io)
94
+ end
95
+
96
+ def post_arguments(io)
97
+ end
98
+
99
+ def source_file_only?
100
+ @context.options[:source_file_only]
101
+ end
102
+
103
+ def print_source_files(io)
104
+ pre_source_files(io)
105
+ @context.source_files.each do |file|
106
+ io.puts(format_file_path(file.full_path))
107
+ end
108
+ post_source_files(io)
109
+ end
110
+
111
+ def pre_source_files(_io)
112
+ end
113
+
114
+ def post_source_files(_io)
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ class SourceFile
5
+ def initialize(root, path)
6
+ @root = root
7
+ @path = path
8
+ end
9
+
10
+ attr_reader :root
11
+ attr_reader :path
12
+
13
+ def full_path
14
+ File.join(@root, @path)
15
+ end
16
+
17
+ def match_ext?(ext_list)
18
+ return false if ext_list.nil? || ext_list.empty?
19
+
20
+ file_ext = File.extname(@path)
21
+ ext_list.any? do |ext|
22
+ (ext[0] == '.' && ext || ".#{ext}") == file_ext
23
+ end
24
+ end
25
+
26
+ def remove_ext(ext_list)
27
+ return self unless match_ext?(ext_list)
28
+
29
+ path = Pathname.new(@path).sub_ext('').to_s
30
+ self.class.new(@root, path)
31
+ end
32
+
33
+ def checksum
34
+ @checksum ||= Digest::MD5.digest(File.read(full_path))
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FLGen
4
+ VERSION = '0.14.0'
5
+ end
data/lib/flgen.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest/md5'
4
+ require 'optparse'
5
+ require 'pathname'
6
+ require_relative 'flgen/version'
7
+ require_relative 'flgen/exceptions'
8
+ require_relative 'flgen/source_file'
9
+ require_relative 'flgen/arguments'
10
+ require_relative 'flgen/file_list'
11
+ require_relative 'flgen/context'
12
+ require_relative 'flgen/formatter'
13
+ require_relative 'flgen/file_list_formatter'
14
+ require_relative 'flgen/cli'
@@ -0,0 +1,6 @@
1
+ define_macro :BAR_0
2
+ define_macro :BAR_1, 1
3
+ compile_argument '-bar_0', tool: :vcs
4
+ runtime_argument '-bar_1', tool: :vcs
5
+ source_file 'sample/bar/bar.sv', from: :root
6
+ file_list 'baz/baz.list.rb', from: :current
data/sample/bar/bar.sv ADDED
@@ -0,0 +1,2 @@
1
+ module bar;
2
+ endmodule
@@ -0,0 +1,8 @@
1
+ include_directory '.'
2
+ if target_tool? :xcelium
3
+ compile_argument '-baz_0'
4
+ runtime_argument '-baz_1'
5
+ end
6
+ unless macro_defined? :NO_BAZ
7
+ source_file 'baz.sv'
8
+ end
@@ -0,0 +1,2 @@
1
+ module baz;
2
+ endmodule
@@ -0,0 +1,5 @@
1
+ compile_argument '-foo_0'
2
+ runtime_argument '-foo_1'
3
+ include_directory 'bar'
4
+ source_file 'foo.sv'
5
+ file_list 'sample/bar/bar.list.rb'
data/sample/foo.sv ADDED
@@ -0,0 +1,2 @@
1
+ module foo;
2
+ endmodule
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flgen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.14.0
5
+ platform: ruby
6
+ authors:
7
+ - Taichi Ishitani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bump
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.12.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.12.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.40.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.40.0
83
+ description: Filelist generator
84
+ email:
85
+ - ishitani@pezy.co.jp
86
+ executables:
87
+ - flgen
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - LICENSE
92
+ - README.md
93
+ - exe/flgen
94
+ - lib/flgen.rb
95
+ - lib/flgen/arguments.rb
96
+ - lib/flgen/cli.rb
97
+ - lib/flgen/context.rb
98
+ - lib/flgen/exceptions.rb
99
+ - lib/flgen/file_list.rb
100
+ - lib/flgen/file_list_formatter.rb
101
+ - lib/flgen/formatter.rb
102
+ - lib/flgen/source_file.rb
103
+ - lib/flgen/version.rb
104
+ - sample/bar/bar.list.rb
105
+ - sample/bar/bar.sv
106
+ - sample/bar/baz/baz.list.rb
107
+ - sample/bar/baz/baz.sv
108
+ - sample/foo.list.rb
109
+ - sample/foo.sv
110
+ homepage: https://github.com/pezy-computing/flgen
111
+ licenses:
112
+ - Apache-2.0
113
+ metadata:
114
+ bug_tracker_uri: https://github.com/pezy-computing/flgen/issues
115
+ rubygems_mfa_required: 'true'
116
+ source_code_uri: https://github.com/pezy-computing/flgen
117
+ wiki_uri: https://github.com/pezy-computing/flgen/wiki
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '3.0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.3.3
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Filelist generator
137
+ test_files: []