Last active
February 10, 2025 17:51
-
-
Save marvhus/09e668437f9896499accf445092236c6 to your computer and use it in GitHub Desktop.
Python script to generate Ninja build script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
# settings | |
src_dir = "src" | |
bin_dir = "bin" | |
target_name = "exe" | |
file = open("build.ninja", "wt") | |
file.write(""" | |
#compile rule | |
cc_flags = -std=c++11 -pedantic -Wall -Wextra -Werror -ggdb | |
rule cc | |
command = g++ $cc_flags -c $in -o $out -MD -MF $out.d | |
depfile = $out.d | |
# linking rule | |
ld_flags = -static | |
rule ld | |
command = g++ $in -o $out $ld_flags | |
""") | |
object_files = [] | |
# create compile commands | |
file.write("\n# compile commands\n") | |
for src_file in os.listdir(src_dir): | |
if not src_file.endswith('.cpp') and not src_file.endswith('.c') and not src_file.endswith('.cc'): | |
continue | |
basename = os.path.splitext(src_file)[0] | |
obj_file = f"{bin_dir}/obj/{basename}.o" | |
file.write(f"build {obj_file}: cc {src_dir}/{src_file}\n") | |
object_files.append(obj_file) | |
# create link command | |
file.write("\n# link command\n") | |
file.write(f"build {bin_dir}/{target_name}: ld") | |
for obj_file in object_files: | |
file.write(f" {obj_file}") | |
file.write("\n") | |
# close file | |
file.close() | |
# make sure there is a bin directory | |
if not os.path.exists(bin_dir): | |
os.makedirs(bin_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment