emsdk-env 0.2.0 → 0.3.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.
@@ -1,11 +1,11 @@
1
1
  /*!
2
2
  * name: emsdk-env
3
- * version: 0.2.0
3
+ * version: 0.3.0
4
4
  * description: Emscripten environment builder
5
5
  * author: Kouji Matsui (@kekyo@mi.kekyo.net)
6
6
  * license: MIT
7
7
  * repository.url: https://github.com/kekyo/emsdk-env
8
- * git.commit.hash: e7a4478d462904c71937847aef2de1f460ffc0d5
8
+ * git.commit.hash: 4a03739b8c88111950fa216a7c10c7bfd65ea239
9
9
  */
10
10
 
11
11
  import { mkdir, access, constants, mkdtemp, rename, rm } from "fs/promises";
@@ -530,7 +530,7 @@ const createBuildId = () => {
530
530
  const seq = String(buildSequence).padStart(4, "0");
531
531
  return `${timestamp}_${seq}_${process.pid}`;
532
532
  };
533
- const ensureArray = (value) => value ? [...value] : [];
533
+ const ensureArray = (value) => value != null ? value : [];
534
534
  const normalizePrepareOptions = (options) => {
535
535
  const { targetVersion, ...rest } = options != null ? options : {};
536
536
  return {
@@ -570,9 +570,15 @@ const resolveOutFile = (outFile, env, outDir) => {
570
570
  const expanded = expandPlaceholders(outFile, env, "outFile");
571
571
  return resolvePath(outDir, expanded);
572
572
  };
573
- const resolveSourcePatterns = (patterns, env, srcDir) => {
574
- const expanded = expandArray(patterns, env, "sources");
575
- return expanded.map((value) => resolvePath(srcDir, value));
573
+ const resolveSourcesFromPatterns = async (patterns, env, srcDir, label) => {
574
+ const expanded = expandArray(patterns, env, label);
575
+ const resolvedPatterns = expanded.map((value) => resolvePath(srcDir, value));
576
+ const results = await Promise.all(
577
+ resolvedPatterns.map((pattern) => glob(pattern, { nodir: true }))
578
+ );
579
+ const sources = results.flat();
580
+ sources.sort();
581
+ return sources;
576
582
  };
577
583
  const buildDefineFlags = (defines) => Object.entries(defines).map(([key, value]) => `-D${key}=${String(value)}`);
578
584
  const buildExportFlags = (exports$1) => {
@@ -594,17 +600,37 @@ const resolveTargetOutFile = (targetName, targetOutFile, env, outDir) => {
594
600
  };
595
601
  const resolveTargetSources = async (targetSources, env, srcDir) => {
596
602
  const patterns = targetSources && targetSources.length > 0 ? targetSources : [join(srcDir, "**", "*.c"), join(srcDir, "**", "*.cpp")];
597
- const resolvedPatterns = resolveSourcePatterns(patterns, env, srcDir);
598
- const results = await Promise.all(
599
- resolvedPatterns.map((pattern) => glob(pattern, { nodir: true }))
603
+ return resolveSourcesFromPatterns(patterns, env, srcDir, "sources");
604
+ };
605
+ const toSafeObjectName = (rootDir, sourcePath, groupIndex) => {
606
+ const baseName = relative(rootDir, sourcePath).replace(/[\\/]/g, "_").replace(/[^A-Za-z0-9._-]/g, "_");
607
+ if (groupIndex === void 0) {
608
+ return baseName;
609
+ }
610
+ return `${baseName}__g${groupIndex}`;
611
+ };
612
+ const dedupeSources = (sources) => {
613
+ const seen = /* @__PURE__ */ new Set();
614
+ const deduped = [];
615
+ for (const source of sources) {
616
+ if (seen.has(source)) {
617
+ continue;
618
+ }
619
+ seen.add(source);
620
+ deduped.push(source);
621
+ }
622
+ return deduped;
623
+ };
624
+ const buildCompileArgs = (options, includeDirs, defines, env, rootDir) => {
625
+ const resolvedOptions = expandArray(options, env, "options");
626
+ const includeArgs = resolveIncludeDirs(includeDirs, env, rootDir).map(
627
+ (dir) => `-I${dir}`
600
628
  );
601
- const sources = results.flat();
602
- sources.sort();
603
- return sources;
629
+ const defineArgs = buildDefineFlags(resolveDefines(defines, env));
630
+ return { resolvedOptions, includeArgs, defineArgs };
604
631
  };
605
- const toSafeObjectName = (rootDir, sourcePath) => relative(rootDir, sourcePath).replace(/[\\/]/g, "_").replace(/[^A-Za-z0-9._-]/g, "_");
606
632
  const buildWasm = async (options) => {
607
- var _a, _b, _c, _d, _e, _f, _g, _h;
633
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
608
634
  if (!options) {
609
635
  throw new TypeError("options must be provided.");
610
636
  }
@@ -662,10 +688,6 @@ const buildWasm = async (options) => {
662
688
  const outFiles = {};
663
689
  try {
664
690
  for (const [targetName, target] of targets) {
665
- const mergedOptions = [
666
- ...ensureArray(common.options),
667
- ...ensureArray(target.options)
668
- ];
669
691
  const mergedLinkOptions = [
670
692
  ...ensureArray(common.linkOptions),
671
693
  ...ensureArray(target.linkOptions)
@@ -674,11 +696,16 @@ const buildWasm = async (options) => {
674
696
  ...ensureArray(common.exports),
675
697
  ...ensureArray(target.exports)
676
698
  ];
677
- const mergedIncludeDirs = [
699
+ const baseCompileOptions = [
700
+ ...ensureArray(common.options),
701
+ ...ensureArray(target.options)
702
+ ];
703
+ const baseIncludeDirs = [
678
704
  ...ensureArray(common.includeDirs),
679
705
  ...ensureArray(target.includeDirs)
680
706
  ];
681
- const mergedDefines = mergeDefines(common.defines, target.defines);
707
+ const baseDefines = mergeDefines(common.defines, target.defines);
708
+ const sourceGroups = (_i = target.sourceGroups) != null ? _i : [];
682
709
  const targetEnv = {
683
710
  ...envWithDirs,
684
711
  TARGET_NAME: targetName
@@ -695,13 +722,35 @@ const buildWasm = async (options) => {
695
722
  targetEnv,
696
723
  srcDir
697
724
  );
698
- if (sources.length === 0) {
725
+ const groupSources = sourceGroups.map(() => []);
726
+ const groupSourceSet = /* @__PURE__ */ new Set();
727
+ for (let index = 0; index < sourceGroups.length; index += 1) {
728
+ const group = sourceGroups[index];
729
+ if (!group) {
730
+ continue;
731
+ }
732
+ const resolved = await resolveSourcesFromPatterns(
733
+ group.sources,
734
+ targetEnv,
735
+ srcDir,
736
+ `sourceGroups[${index}].sources`
737
+ );
738
+ const deduped = dedupeSources(resolved);
739
+ groupSources[index] = deduped;
740
+ for (const source of deduped) {
741
+ groupSourceSet.add(source);
742
+ }
743
+ }
744
+ const baseSources = sources.filter(
745
+ (source) => !groupSourceSet.has(source)
746
+ );
747
+ const groupedSources = groupSources.flat();
748
+ if (baseSources.length + groupedSources.length === 0) {
699
749
  throw new Error(`No sources matched for target: ${targetName}`);
700
750
  }
701
751
  const targetBuildDir = resolve(buildRunDir, targetName);
702
752
  await rm(targetBuildDir, { recursive: true, force: true });
703
753
  await ensureDirectory(targetBuildDir);
704
- const resolvedOptions = expandArray(mergedOptions, targetEnv, "options");
705
754
  const resolvedLinkOptions = expandArray(
706
755
  mergedLinkOptions,
707
756
  targetEnv,
@@ -709,31 +758,49 @@ const buildWasm = async (options) => {
709
758
  );
710
759
  const resolvedExports = expandArray(mergedExports, targetEnv, "exports");
711
760
  const exportArgs = buildExportFlags(resolvedExports);
712
- const includeArgs = resolveIncludeDirs(
713
- mergedIncludeDirs,
761
+ const baseCompileArgs = buildCompileArgs(
762
+ baseCompileOptions,
763
+ baseIncludeDirs,
764
+ baseDefines,
714
765
  targetEnv,
715
766
  rootDir
716
- ).map((dir) => `-I${dir}`);
717
- const defineArgs = buildDefineFlags(
718
- resolveDefines(mergedDefines, targetEnv)
719
767
  );
768
+ const groupCompileArgs = sourceGroups.map((group) => {
769
+ var _a2;
770
+ const groupOptions = [
771
+ ...baseCompileOptions,
772
+ ...ensureArray(group == null ? void 0 : group.options)
773
+ ];
774
+ const groupIncludeDirs = [
775
+ ...baseIncludeDirs,
776
+ ...ensureArray(group == null ? void 0 : group.includeDirs)
777
+ ];
778
+ const groupDefines = mergeDefines(baseDefines, (_a2 = group == null ? void 0 : group.defines) != null ? _a2 : {});
779
+ return buildCompileArgs(
780
+ groupOptions,
781
+ groupIncludeDirs,
782
+ groupDefines,
783
+ targetEnv,
784
+ rootDir
785
+ );
786
+ });
720
787
  logger.info(`Compiling target: ${targetName}`);
721
- const compileSource = async (source) => {
722
- const objectName = toSafeObjectName(rootDir, source);
788
+ const compileSource = async (source, args, groupIndex) => {
789
+ const objectName = toSafeObjectName(rootDir, source, groupIndex);
723
790
  const outputObject = resolve(targetBuildDir, `${objectName}.o`);
724
- const args = [
791
+ const compileArgs = [
725
792
  "-c",
726
793
  source,
727
794
  "-o",
728
795
  outputObject,
729
- ...resolvedOptions,
730
- ...includeArgs,
731
- ...defineArgs
796
+ ...args.resolvedOptions,
797
+ ...args.includeArgs,
798
+ ...args.defineArgs
732
799
  ];
733
- logger.debug(`emcc ${args.join(" ")}`);
800
+ logger.debug(`emcc ${compileArgs.join(" ")}`);
734
801
  await runCommandWithEnv(
735
802
  emccCommand,
736
- args,
803
+ compileArgs,
737
804
  rootDir,
738
805
  buildEnv,
739
806
  emsdkOptions.signal
@@ -742,12 +809,52 @@ const buildWasm = async (options) => {
742
809
  };
743
810
  const buildObjectsSequential = async () => {
744
811
  const objectFiles2 = [];
745
- for (const source of sources) {
746
- objectFiles2.push(await compileSource(source));
812
+ for (const source of baseSources) {
813
+ objectFiles2.push(
814
+ await compileSource(source, baseCompileArgs, void 0)
815
+ );
816
+ }
817
+ for (let index = 0; index < groupSources.length; index += 1) {
818
+ const sourcesInGroup = groupSources[index];
819
+ if (!sourcesInGroup) {
820
+ continue;
821
+ }
822
+ const groupArgs = groupCompileArgs[index];
823
+ if (!groupArgs) {
824
+ continue;
825
+ }
826
+ for (const source of sourcesInGroup) {
827
+ objectFiles2.push(await compileSource(source, groupArgs, index));
828
+ }
747
829
  }
748
830
  return objectFiles2;
749
831
  };
750
- const objectFiles = parallel ? await Promise.all(sources.map((source) => compileSource(source))) : await buildObjectsSequential();
832
+ const compileJobs = [];
833
+ for (const source of baseSources) {
834
+ compileJobs.push({
835
+ source,
836
+ args: baseCompileArgs,
837
+ groupIndex: void 0
838
+ });
839
+ }
840
+ for (let index = 0; index < groupSources.length; index += 1) {
841
+ const sourcesInGroup = groupSources[index];
842
+ if (!sourcesInGroup) {
843
+ continue;
844
+ }
845
+ const groupArgs = groupCompileArgs[index];
846
+ if (!groupArgs) {
847
+ continue;
848
+ }
849
+ for (const source of sourcesInGroup) {
850
+ compileJobs.push({ source, args: groupArgs, groupIndex: index });
851
+ }
852
+ }
853
+ const objectFiles = parallel ? await Promise.all(
854
+ compileJobs.map(
855
+ (job) => compileSource(job.source, job.args, job.groupIndex)
856
+ )
857
+ ) : await buildObjectsSequential();
751
858
  logger.info(`Linking target: ${targetName}`);
752
859
  const linkArgs = [
753
860
  ...objectFiles,
@@ -780,4 +887,4 @@ export {
780
887
  buildWasm as b,
781
888
  prepareEmsdk as p
782
889
  };
783
- //# sourceMappingURL=build-qp0IEBnb.js.map
890
+ //# sourceMappingURL=build-CYHeaOdc.js.map