forked from lzh9102/makegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakegen.py
More file actions
executable file
·178 lines (154 loc) · 6.35 KB
/
Copy pathmakegen.py
File metadata and controls
executable file
·178 lines (154 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# quickly generate makefile for c/c++ source files
import sys
import os
import json
import argparse
from make_options import MakeOptions
from generators import list_generators
from generators import GENERATORS
from rule_generator import RULE_GENERATORS
from rule_generator import get_header_rule_generator
from hash import calc_hash
def build_argument_parser():
parser = argparse.ArgumentParser(
description="Generate makefile from source files.")
parser.add_argument("--files", type=str, nargs="+",
help="source files")
parser.add_argument("-o", "--output", type=str, default=None,
help="the name of the output executable or library")
parser.add_argument("-f", "--format", default="make",
help="format of the output makefile.")
parser.add_argument("-l", action="append", dest="link_libraries",
help="link to a library")
parser.add_argument("-D", action="append", dest="defines",
help="add a preprocessor definition")
parser.add_argument("-L", action="append", dest="library_paths",
help="add a library path")
parser.add_argument("-I", action="append", dest="include_paths",
help="add a include path")
parser.add_argument("-n", "--name", type=str, default="my_project",
help="name of the project")
parser.add_argument("--cflags", type=str, default=None,
help="c compiler flags")
parser.add_argument("--cxxflags", type=str, default=None,
help="c++ compiler flags")
parser.add_argument("--ldflags", type=str, default=None,
help="linker flags")
return parser
def build_make_options(arg):
options = MakeOptions()
options.project_name = arg.name
options.sources = arg.files
options.output = "a.out"
if arg.output:
options.output = arg.output
if arg.link_libraries:
options.link_libraries = arg.link_libraries
if arg.defines:
options.defines = arg.defines
if arg.library_paths:
options.library_paths = arg.library_paths
if arg.include_paths:
options.include_paths = arg.include_paths
if arg.cflags:
options.cflags = arg.cflags
if arg.cxxflags:
options.cxxflags = arg.cxxflags
if arg.ldflags:
options.ldflags = arg.ldflags
return options
def load_files_from_path(path):
extensions = []
sources = []
header_include_paths = []
header_ext = get_header_rule_generator().handled_extensions()
for gen in RULE_GENERATORS:
for ext in gen.handled_extensions():
extensions.append(ext)
for root, subdirs, files in os.walk(path):
for f in files:
filename, ext = os.path.splitext(f)
if ext[1:] in extensions:
if (root.startswith("./")):
sources.append(root[2:]+"/"+f)
else:
sources.append(root+"/"+f)
if ext[1:] in header_ext:
header_include_paths.append(root)
include_paths = set(header_include_paths)
return sources, include_paths
def read_custom_section_from_makefile(options):
custom_section_header = "################################################################\n\
###### You can put customized rules below this comment ######\n\
###### It will not be deleted if you rebuild the Makefile ######\n\
################################################################"
if os.path.isfile("Makefile"):
# makefile exists
with open("Makefile", "r") as mkfile:
content = mkfile.read()
index = content.find(custom_section_header)
if index == -1:
options.custom_section = custom_section_header + "\n\n\n\n"
else:
options.custom_section = content[index:]
else:
# makefile doesn't exist
options.custom_section = custom_section_header + "\n\n\n\n"
def check_for_makegen_file(arg, options):
if not os.path.isfile('makegen.json'):
print('No makegen.json found')
return
with open('makegen.json', 'r') as config:
raw = json.loads(config.read())
data = {k.upper():v for k,v in raw.items()}
if "CC" in data:
options.c_compiler = data["CC"]
if "CXX" in data:
options.cpp_compiler = data["CXX"]
if "AS" in data:
options.as_compiler = data["AS"]
if "ASFLAGS" in data:
options.asflags = data["ASFLAGS"]
if "CFLAGS" in data:
options.cflags = data["CFLAGS"]
if "LDFLAGS" in data:
options.ldflags = data["LDFLAGS"]
if "OUTPUT" in data:
options.output = data["OUTPUT"]
if "LINK_LIBS" in data:
options.link_libraries = data["LINK_LIBS"]
if "PATH_LIBS" in data:
options.library_paths = data["PATH_LIBS"]
if "INCLUDE_PATHS" in data:
options.include_paths = data["INCLUDE_PATHS"]
if "CXXFLAGS" in data:
options.cxxflags = data["CXXFLAGS"]
if "DEFINES" in data:
options.defines = data["DEFINES"]
if "ROOT_DIR" in data:
options.root_dir = data["ROOT_DIR"]
options.sources, inc_paths = load_files_from_path(options.root_dir)
options.include_paths += inc_paths
if "PROJECT_NAME" in data:
options.project_name = data["PROJECT_NAME"]
if "OUTPUT" in data:
options.output = data["OUTPUT"]
if "OBJDIR" in data:
options.objdir = data["OBJDIR"]
if __name__ == "__main__":
parser = build_argument_parser()
arg = parser.parse_args()
options = build_make_options(arg)
check_for_makegen_file(arg, options)
options.hash = calc_hash(options.sources)
read_custom_section_from_makefile(options)
if not options.root_dir and not options.sources:
print("error: specify files on the command line or supply them with \"ROOT_DIR\" in the makegen.json file")
exit(1)
if arg.format in GENERATORS:
generator = GENERATORS[arg.format]
generator.generate(options)
else:
print("error: unknown format %1s" % arg.format)
print("supported formats are: %1s" % list_generators())