Skip to content

Commit 1904b30

Browse files
committed
tools: make js2c.py usable for other build systems
1 parent 31156a7 commit 1904b30

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

tools/js2c.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,19 @@ def GetDefinition(var, source, step=30):
119119
return definition, len(code_points)
120120

121121

122-
def AddModule(filename, definitions, initializers):
122+
def AddModule(root, filename, definitions, initializers):
123123
code = ReadFile(filename)
124-
name = NormalizeFileName(filename)
124+
name = NormalizeFileName(root, filename)
125125
slug = SLUGGER_RE.sub('_', name)
126126
var = slug + '_raw'
127127
definition, size = GetDefinition(var, code)
128128
initializer = INITIALIZER.format(name, var, size)
129129
definitions.append(definition)
130130
initializers.append(initializer)
131131

132-
def NormalizeFileName(filename):
132+
def NormalizeFileName(root, filename):
133+
if root:
134+
filename = os.path.relpath(filename, root)
133135
split = filename.split('/')
134136
if split[0] == 'deps':
135137
split = ['internal'] + split
@@ -140,15 +142,15 @@ def NormalizeFileName(filename):
140142
return os.path.splitext(filename)[0]
141143

142144

143-
def JS2C(source_files, target):
145+
def JS2C(root, source_files, target):
144146
# Build source code lines
145147
definitions = []
146148
initializers = []
147149

148150
for filename in source_files['.js']:
149-
AddModule(filename, definitions, initializers)
151+
AddModule(root, filename, definitions, initializers)
150152
for filename in source_files['.mjs']:
151-
AddModule(filename, definitions, initializers)
153+
AddModule(root, filename, definitions, initializers)
152154

153155
config_def, config_size = handle_config_gypi(source_files['config.gypi'])
154156
definitions.append(config_def)
@@ -215,6 +217,10 @@ def main():
215217
'--directory',
216218
default=None,
217219
help='input file directory')
220+
parser.add_argument(
221+
'--root',
222+
default=None,
223+
help='root directory containing the sources')
218224
parser.add_argument('--verbose', action='store_true', help='output file')
219225
parser.add_argument('sources', nargs='*', help='input files')
220226
options = parser.parse_args()
@@ -231,9 +237,10 @@ def main():
231237
# Should have exactly 3 types: `.js`, `.mjs` and `.gypi`
232238
assert len(source_files) == 3
233239
# Currently config.gypi is the only `.gypi` file allowed
234-
assert source_files['.gypi'] == ['config.gypi']
240+
assert len(source_files['.gypi']) == 1
241+
assert os.path.basename(source_files['.gypi'][0]) == 'config.gypi'
235242
source_files['config.gypi'] = source_files.pop('.gypi')[0]
236-
JS2C(source_files, options.target)
243+
JS2C(options.root, source_files, options.target)
237244

238245

239246
if __name__ == "__main__":

0 commit comments

Comments
 (0)