Debian Codegraph @

cmake/scripts/generate-cmakefiles.py

raw
1 # Copyright (C) 2010-16 Garth N. Wells
2 #
3 # This file is part of DOLFIN.
4 #
5 # DOLFIN is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # DOLFIN is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Generate CMakeLists.txt files in demo and test directories
19 # This script should be run from the top level directory.
20 #
21 # Modified by Anders Logg 2013, 2014
22
23 import os
24 import subprocess
25
26 cmakelists_str = \
27 """# This file is automatically generated by running
28 #
29 # cmake/scripts/generate-cmakefiles
30 #
31 # Require CMake 3.5
32 cmake_minimum_required(VERSION 3.5)
33
34 set(PROJECT_NAME %(project_name)s)
35 project(${PROJECT_NAME})
36
37 # Set CMake behavior
38 cmake_policy(SET CMP0004 NEW)
39
40 # Get DOLFIN configuration data (DOLFINConfig.cmake must be in
41 # DOLFIN_CMAKE_CONFIG_PATH)
42 find_package(DOLFIN REQUIRED)
43
44 include(${DOLFIN_USE_FILE})
45
46 # Default build type (can be overridden by user)
47 if (NOT CMAKE_BUILD_TYPE)
48 set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
49 "Choose the type of build, options are: Debug MinSizeRel Release RelWithDebInfo." FORCE)
50 endif()
51
52 # Do not throw error for 'multi-line comments' (these are typical in
53 # rst which includes LaTeX)
54 include(CheckCXXCompilerFlag)
55 CHECK_CXX_COMPILER_FLAG("-Wno-comment" HAVE_NO_MULTLINE)
56 if (HAVE_NO_MULTLINE)
57 set(CMAKE_CXX_FLAGS "-Wno-comment ${CMAKE_CXX_FLAGS}")
58 endif()
59
60 # Executable
61 %(executables)s
62
63 # Target libraries
64 %(target_libraries)s
65
66 # Test targets
67 set(test_parameters -np 3 ${MPIEXEC_PARAMS} "./${PROJECT_NAME}")
68 add_test(NAME ${PROJECT_NAME}_mpi COMMAND "mpirun" ${test_parameters})
69 add_test(NAME ${PROJECT_NAME}_serial COMMAND ${PROJECT_NAME})
70 """
71
72 executable_str = "add_executable(%s %s)"
73 target_link_libraries_str = "target_link_libraries(%s dolfin)"
74
75 # Subdirectories
76 sub_directories = ['demo', 'bench']
77 # Prefix map for subdirectories
78 executable_prefixes = dict(demo="demo_",
79 bench="bench_")
80
81 # Main file name map for subdirectories
82 main_file_names = dict(demo=set(["main.cpp", "main.cpp.rst"]),
83 bench=set(["main.cpp"]))
84
85 # Projects that use custom CMakeLists.txt (shouldn't overwrite)
86 #exclude_projects = [os.path.join('demo', 'undocumented', 'plot-qt')]
87 exclude_projects = []
88
89 def generate_cmake_files(subdirectory, generated_files):
90 """Search for C++ code and write CMakeLists.txt files"""
91 cwd = os.getcwd()
92 executable_prefix = executable_prefixes[subdirectory]
93 main_file_name = main_file_names[subdirectory]
94 for root, dirs, files in os.walk(cwd + "/" + subdirectory):
95
96 executable_names = set()
97
98 # For 'cpp' directories, get list of .cpp files
99 if "cpp" in dirs:
100 cpp_files = set()
101 program_dir = root + "/cpp"
102 program_name = os.path.split(root)[-1]
103
104 skip = False
105 for exclude in exclude_projects :
106 if exclude in root :
107 skip = True
108
109 if skip :
110 print("Skipping custom CMakeLists.txt file:", root)
111 continue
112
113 if executable_prefix == "bench_":
114 program_name = program_dir[program_dir.find("bench")+6:].replace(os.path.sep, "_")
115
116 name_forms = dict(
117 project_name=executable_prefix + program_name,
118 executables="NOT_SET",
119 target_libraries="NOT_SET")
120 for f in os.listdir(program_dir):
121 filename, extension = os.path.splitext(f)
122 if extension == ".cpp":
123 cpp_files.add(f)
124 if ".cpp.rst" in f:
125 cpp_files.add(filename)
126
127 # If no .cpp continue
128 if not cpp_files:
129 continue
130
131 # Ensure deterministic output across py2/py3
132 cpp_files = sorted(cpp_files)
133
134 # Name of demo and cpp source files
135 if not main_file_name.isdisjoint(cpp_files):
136
137 # If directory contains a main file we assume that
138 # only one executable should be generated for this
139 # directory and all other .cpp files should be linked
140 # to this
141 name_forms["executables"] = executable_str % \
142 ("${PROJECT_NAME}",
143 ' '.join(cpp_files))
144 name_forms["target_libraries"] = target_link_libraries_str % \
145 "${PROJECT_NAME}"
146 else:
147 # If no main file in source files, we assume each
148 # source should be compiled as an executable
149 name_forms["executables"] = "\n".join(\
150 executable_str % (executable_prefix + f.replace(".cpp", ""), f) \
151 for f in cpp_files)
152 name_forms["target_libraries"] = "\n".join(\
153 target_link_libraries_str % (\
154 executable_prefix + f.replace(".cpp", "")) \
155 for f in cpp_files)
156
157 # Check for duplicate executable names
158 if program_name not in executable_names:
159 executable_names.add(program_name)
160 else:
161 print("Warning: duplicate executable names found when generating CMakeLists.txt files.")
162
163 # Write file
164 filename = os.path.join(program_dir, "CMakeLists.txt")
165 generated_files.append(filename)
166 with open(filename, "w") as f:
167 f.write(cmakelists_str % name_forms)
168
169
170 # Generate CMakeLists.txt files for all subdirectories
171 generated_files = []
172 for subdirectory in sub_directories:
173 generate_cmake_files(subdirectory, generated_files)
174
175 # Print list of generated files
176 print("The following files were generated:")
177 print("\n".join(generated_files))