-
Notifications
You must be signed in to change notification settings - Fork 4
/
ct-create-cmakelists
executable file
·124 lines (107 loc) · 4.33 KB
/
ct-create-cmakelists
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
#!/usr/bin/python3
# vim: set filetype=python:
import sys
from io import open
import configargparse
import ct.apptools
import ct.wrappedos
import ct.headerdeps
import ct.magicflags
import ct.hunter
import ct.namer
class CMakefileCreator:
""" Create a CMakefile.txt based on the filename, --static and --dynamic command line options """
def __init__(self, args):
self.args = args
self.namer = ct.namer.Namer(args)
headerdeps = ct.headerdeps.create(args)
magicparser = ct.magicflags.create(args, headerdeps)
self.hunter = ct.hunter.Hunter(args, headerdeps, magicparser)
@staticmethod
def add_arguments(cap):
ct.apptools.add_target_arguments_ex(cap)
ct.apptools.add_link_arguments(cap)
# Don't add the output directory arguments
# The Namer will do it and get the hash correct
# ct.utils.add_output_directory_arguments(cap, variant)
ct.namer.Namer.add_arguments(cap)
ct.hunter.add_arguments(cap)
def create(self):
with open("CMakeLists.txt", mode="w", encoding="utf-8") as cmf:
cmf.write("# CMakeLists.txt generated by ct-create-cmakelists\n")
cmf.write("cmake_minimum_required (VERSION 2.8)\n\n")
# TODO: Set "project" to be the name of the cwd
cmf.write("project (CompileTools)\n")
cmf.write("".join(['set(CMAKE_CXX_COMPILER "', self.args.CXX, '")\n']))
cmf.write(
"".join(
[
'set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ',
self.args.CXXFLAGS.replace("\\", "\\\\\\"),
'")\n\n',
]
)
)
cmf.write("include_directories(\n ")
cmf.write("\n ".join(self.args.INCLUDE))
cmf.write("\n)\n")
for source_filename in self.args.filename:
src_realpath = ct.wrappedos.realpath(source_filename)
complete_sources = self.hunter.required_source_files(src_realpath)
exe_name = self.namer.executable_name(src_realpath)
exe_source_files_cmake_name = exe_name.upper() + "_SOURCE_FILES"
cmf.write("\nset(" + exe_source_files_cmake_name + "\n ")
cmf.write("\n ".join(list(complete_sources)))
cmf.write("\n)\n\n")
cmf.write(
"".join(
[
"add_executable(",
exe_name,
" ${",
exe_source_files_cmake_name,
"})\n\n",
]
)
)
cmf.write(
" ".join(
[
"target_include_directories(",
exe_name,
"PUBLIC",
ct.wrappedos.dirname(src_realpath),
")\n",
]
)
)
cmf.write(
" ".join(
[
"install (TARGETS",
exe_name,
"DESTINATION",
self.namer.executable_dir(src_realpath),
")\n\n",
]
)
)
def main(argv=None):
if argv is None:
argv = sys.argv
variant = ct.configutils.extract_variant(argv)
config_files = ct.configutils.config_files_from_variant()
cap = configargparse.getArgumentParser(
description="Create a CMakefile.txt that will compile the given source file into an executable (or library). Each given file is assumed to be compiled into a separate executable and/or library.",
formatter_class=configargparse.ArgumentDefaultsHelpFormatter,
auto_env_var_prefix="",
default_config_files=config_files,
args_for_setting_config_path=["-c", "--config"],
ignore_unknown_config_file_keys=True,
)
CMakefileCreator.add_arguments(cap)
args = ct.apptools.parseargs(cap, argv)
cmakefile_creator = CMakefileCreator(args)
cmakefile_creator.create()
if __name__ == "__main__":
main()