-
Notifications
You must be signed in to change notification settings - Fork 4
/
make_CMakeLists.py
176 lines (130 loc) · 4.14 KB
/
make_CMakeLists.py
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
import copy
import os
def is_extensionless_Eigen_header(filepath):
"""Returns true if the provided file resides in
include/casm/external/Eigen, which contains header files
that don't have an extension like .h
Parameters
----------
filepath : path to file, relative to git root
Returns
-------
bool
"""
parent, f = os.path.split(filepath)
return parent == "include/casm/external/Eigen"
def header_extensions():
"""List of extensions that are considered header files:
.h, .hh, .hpp
Returns
-------
list of str
"""
return [".h", ".hh", ".hpp"]
def source_extensions():
"""List of extensions that are considered source files:
.c, .cc, .cxx, .cpp
Returns
-------
list of str
"""
return [".c", ".cc", ".cxx", ".cpp", ".C"]
def has_header_extension(filepath):
"""Returns true of the file has an extension such as
.h, .hh, etc as defined by header_extensions()
Parameters
----------
filepath : path to file
Returns
-------
bool
"""
for ext in header_extensions():
if filepath.endswith(ext):
return True
return False
def has_source_extension(filepath):
"""Returns true of the file has an extension such as
.c, .cc, etc as defined by source_extensions()
Parameters
----------
filepath : path to file
Returns
-------
bool
"""
for ext in source_extensions():
if filepath.endswith(ext):
return True
return False
def header_and_source_extensions():
return header_extensions() + source_extensions()
def header_files(search_root):
files_by_dir = [
(dirpath, files)
for dirpath, dirnames, files in os.walk(search_root, followlinks=True)
]
files = [
os.path.join(dirpath, file) for dirpath, files in files_by_dir for file in files
]
_header_files = [
file
for file in files
if is_extensionless_Eigen_header(file) or has_header_extension(file)
]
return sorted(_header_files)
def source_files(search_root):
files = [
(dirpath, files)
for dirpath, dirnames, files in os.walk(search_root, followlinks=True)
]
_source_files = [
os.path.join(d, f) for d, fs in files for f in fs if has_source_extension(f)
]
return sorted(_source_files)
def libcasm_testing_source_files(search_dir):
files = [
os.path.join(search_dir, file)
for file in os.listdir(search_dir)
if file != "gtest_main_run_all.cpp" and has_source_extension(file)
]
return sorted(files)
def unit_test_source_files(search_dir, additional):
files = copy.copy(additional)
files += [
os.path.join(search_dir, file)
for file in os.listdir(search_dir)
if has_source_extension(file)
]
return sorted(files)
def as_cmake_file_strings(files):
cmake_file_strings = ""
for file in files:
cmake_file_strings += " ${PROJECT_SOURCE_DIR}/" + str(file) + "\n"
return cmake_file_strings
### make CMakeLists.txt from CMakeLists.txt.in ###
with open("CMakeLists.txt.in", "r") as f:
cmakelists = f.read()
files = header_files("include")
cmake_file_strings = as_cmake_file_strings(files)
cmakelists = cmakelists.replace("@header_files@", cmake_file_strings)
files = source_files("src")
cmake_file_strings = as_cmake_file_strings(files)
cmakelists = cmakelists.replace("@source_files@", cmake_file_strings)
with open("CMakeLists.txt", "w") as f:
f.write(cmakelists)
### make tests/CMakeLists.txt from tests/CMakeLists.txt.in ###
os.chdir("tests")
with open("CMakeLists.txt.in", "r") as f:
cmakelists = f.read()
files = libcasm_testing_source_files("unit")
cmake_file_strings = as_cmake_file_strings(files)
cmakelists = cmakelists.replace("@libcasm_testing_source_files@", cmake_file_strings)
additional = ["unit/gtest_main_run_all.cpp"]
files = unit_test_source_files("unit/clexulator", additional)
cmake_file_strings = as_cmake_file_strings(files)
cmakelists = cmakelists.replace(
"@casm_unit_clexulator_source_files@", cmake_file_strings
)
with open("CMakeLists.txt", "w") as f:
f.write(cmakelists)