|
| 1 | +// @ts-check |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +const SDK_DIR = "simplicity_sdk_2024.12.1"; |
| 6 | + |
| 7 | +const filename = (await fs.readdir("build")).find((f) => f.endsWith(".mak")); |
| 8 | +if (!filename) { |
| 9 | + console.error("Makefile not found in build directory."); |
| 10 | + process.exit(1); |
| 11 | +} |
| 12 | + |
| 13 | +let makefile = await fs.readFile(`build/${filename}`, "utf8"); |
| 14 | + |
| 15 | +let prefixEnd = makefile.indexOf("SDK Build Rules"); |
| 16 | +prefixEnd = makefile.indexOf("#####\n", prefixEnd) + 6; |
| 17 | + |
| 18 | +const suffixIndex = makefile.lastIndexOf( |
| 19 | + "# Automatically-generated Simplicity Studio Metadata" |
| 20 | +); |
| 21 | + |
| 22 | +const allSources = (await fs.readdir(process.cwd(), { recursive: true })) |
| 23 | + .filter((f) => f.endsWith(".c") || f.endsWith(".S")) |
| 24 | + .filter((f) => !f.split(path.sep).includes("build")); |
| 25 | + |
| 26 | +const projectSources = allSources.filter( |
| 27 | + (f) => !f.split(path.sep).includes(SDK_DIR) |
| 28 | +); |
| 29 | + |
| 30 | +const sdkSources = allSources.filter((f) => |
| 31 | + f.split(path.sep).includes(SDK_DIR) |
| 32 | +); |
| 33 | + |
| 34 | +let fileImports = ""; |
| 35 | +for (const source of sdkSources) { |
| 36 | + let relative = source.slice(source.indexOf(path.sep) + 1); |
| 37 | + let withoutExt = relative.slice(0, relative.lastIndexOf(".")); |
| 38 | + |
| 39 | + fileImports += ` |
| 40 | +$(OUTPUT_DIR)/sdk/${withoutExt}.o: $(COPIED_SDK_PATH)/${relative} |
| 41 | + @$(POSIX_TOOL_PATH)echo 'Building $(COPIED_SDK_PATH)/${relative}' |
| 42 | + @$(POSIX_TOOL_PATH)mkdir -p $(@D) |
| 43 | + $(ECHO)$(CC) $(CFLAGS) -c -o $@ $(COPIED_SDK_PATH)/${relative} |
| 44 | +CDEPS += $(OUTPUT_DIR)/sdk/${withoutExt}.d |
| 45 | +OBJS += $(OUTPUT_DIR)/sdk/${withoutExt}.o |
| 46 | +
|
| 47 | +`; |
| 48 | +} |
| 49 | + |
| 50 | +for (const source of projectSources) { |
| 51 | + let withoutExt = source.slice(0, source.lastIndexOf(".")); |
| 52 | + |
| 53 | + fileImports += ` |
| 54 | +$(OUTPUT_DIR)/project/${withoutExt}.o: ${source} |
| 55 | + @$(POSIX_TOOL_PATH)echo 'Building ${source}' |
| 56 | + @$(POSIX_TOOL_PATH)mkdir -p $(@D) |
| 57 | + $(ECHO)$(CC) $(CFLAGS) -c -o $@ ${source} |
| 58 | +CDEPS += $(OUTPUT_DIR)/project/${withoutExt}.d |
| 59 | +OBJS += $(OUTPUT_DIR)/project/${withoutExt}.o |
| 60 | +
|
| 61 | +`; |
| 62 | +} |
| 63 | + |
| 64 | +makefile = |
| 65 | + makefile.slice(0, prefixEnd) + fileImports + makefile.slice(suffixIndex); |
| 66 | + |
| 67 | +makefile = makefile.replace( |
| 68 | + /^BASE_SDK_PATH = .*$/m, |
| 69 | + `BASE_SDK_PATH = ${path.join(process.cwd(), SDK_DIR)}` |
| 70 | +); |
| 71 | + |
| 72 | +await fs.writeFile(`build/${filename}`, makefile); |
| 73 | +console.log("Makefile patched."); |
0 commit comments