173
|
1 cmake_minimum_required(VERSION 3.9.0)
|
|
2
|
|
3 # RPATH settings on macOS do not affect INSTALL_NAME.
|
|
4 if (POLICY CMP0068)
|
|
5 cmake_policy(SET CMP0068 NEW)
|
|
6 set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
|
|
7 endif()
|
|
8
|
|
9 # Include file check macros honor CMAKE_REQUIRED_LIBRARIES.
|
|
10 if(POLICY CMP0075)
|
|
11 cmake_policy(SET CMP0075 NEW)
|
|
12 endif()
|
|
13
|
|
14 # option() honors normal variables.
|
|
15 if (POLICY CMP0077)
|
|
16 cmake_policy(SET CMP0077 NEW)
|
|
17 endif()
|
|
18
|
|
19 option(LINK_WITH_FIR "Link driver with FIR and LLVM" ON)
|
|
20
|
|
21 # Flang requires C++17.
|
|
22 set(CMAKE_CXX_STANDARD 17)
|
|
23 set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
|
|
24 set(CMAKE_CXX_EXTENSIONS OFF)
|
|
25
|
|
26 set(FLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
|
27
|
|
28 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE)
|
|
29 message(FATAL_ERROR "In-source builds are not allowed. \
|
|
30 Please create a directory and run cmake from there,\
|
|
31 passing the path to this source directory as the last argument.\
|
|
32 This process created the file `CMakeCache.txt' and the directory\
|
|
33 `CMakeFiles'. Please delete them.")
|
|
34 endif()
|
|
35
|
|
36 # Add Flang-centric modules to cmake path.
|
|
37 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
|
|
38 include(AddFlang)
|
|
39
|
|
40 # Check for a standalone build and configure as appropriate from
|
|
41 # there.
|
|
42 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
|
43 message("Building Flang as a standalone project.")
|
|
44 project(Flang)
|
|
45
|
|
46 set(FLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
|
47 if (NOT MSVC_IDE)
|
|
48 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
|
|
49 CACHE BOOL "Enable assertions")
|
|
50 # Assertions follow llvm's configuration.
|
|
51 mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
|
|
52 endif()
|
|
53
|
|
54 # We need a pre-built/installed version of LLVM.
|
|
55 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_PATH}")
|
|
56 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
|
|
57
|
|
58 # If LLVM links to zlib we need the imported targets so we can too.
|
|
59 if(LLVM_ENABLE_ZLIB)
|
|
60 find_package(ZLIB REQUIRED)
|
|
61 endif()
|
|
62
|
|
63 include(CMakeParseArguments)
|
|
64 include(AddLLVM)
|
|
65 include(HandleLLVMOptions)
|
|
66 include(VersionFromVCS)
|
|
67
|
|
68 if(LINK_WITH_FIR)
|
|
69 include(TableGen)
|
|
70 find_package(MLIR REQUIRED CONFIG)
|
|
71 # Use SYSTEM for the same reasons as for LLVM includes
|
|
72 include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
|
|
73 list(APPEND CMAKE_MODULE_PATH ${MLIR_DIR})
|
|
74 include(AddMLIR)
|
|
75 find_program(MLIR_TABLEGEN_EXE "mlir-tblgen" ${LLVM_TOOLS_BINARY_DIR}
|
|
76 NO_DEFAULT_PATH)
|
|
77 endif()
|
|
78
|
|
79 option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
|
|
80 option(LLVM_INSTALL_TOOLCHAIN_ONLY
|
|
81 "Only include toolchain files in the 'install' target." OFF)
|
|
82 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
|
|
83 "Set to ON to force using an old, unsupported host toolchain." OFF)
|
|
84
|
|
85
|
|
86 # Add LLVM include files as if they were SYSTEM because there are complex unused
|
|
87 # parameter issues that may or may not appear depending on the environments and
|
|
88 # compilers (ifdefs are involved). This allows warnings from LLVM headers to be
|
|
89 # ignored while keeping -Wunused-parameter a fatal error inside f18 code base.
|
|
90 # This may have to be fine-tuned if flang headers are consider part of this
|
|
91 # LLVM_INCLUDE_DIRS when merging in the monorepo (Warning from flang headers
|
|
92 # should not be suppressed).
|
|
93 include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
|
|
94 add_definitions(${LLVM_DEFINITIONS})
|
|
95
|
|
96 # LLVM's cmake configuration files currently sneak in a c++11 flag.
|
|
97 # We look for it here and remove it from Flang's compile flags to
|
|
98 # avoid some mixed compilation flangs (e.g. -std=c++11 ... -std=c++17).
|
|
99 if (DEFINED LLVM_CXX_STD)
|
|
100 message("LLVM configuration set a C++ standard: ${LLVM_CXX_STD}")
|
|
101 if (NOT LLVM_CXX_STD EQUAL "c++17")
|
|
102 message("Flang: Overriding LLVM's 'cxx_std' setting...")
|
|
103 message(" removing '-std=${LLVM_CXX_STD}'")
|
|
104 message(" CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
|
|
105 string(REPLACE " -std=${LLVM_CXX_STD}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
|
106 message(" [NEW] CMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'")
|
|
107 endif()
|
|
108 endif()
|
|
109
|
|
110 link_directories("${LLVM_LIBRARY_DIR}")
|
|
111
|
|
112 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
|
113 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
|
|
114 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
|
|
115 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
|
|
116 ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
|
|
117
|
|
118 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
|
|
119 set(LLVM_EXTERNAL_LIT "${LLVM_TOOLS_BINARY_DIR}/llvm-lit" CACHE STRING "Command used to spawn lit")
|
|
120
|
|
121 option(FLANG_INCLUDE_TESTS
|
|
122 "Generate build targets for the Flang unit tests."
|
|
123 ON)
|
|
124 add_custom_target(check-all DEPENDS check-flang)
|
|
125 if (LLVM_BUILD_DOCS)
|
|
126 add_custom_target(doxygen ALL)
|
|
127 endif()
|
|
128
|
|
129 else()
|
|
130 option(FLANG_INCLUDE_TESTS
|
|
131 "Generate build targets for the Flang unit tests."
|
|
132 ${LLVM_INCLUDE_TESTS})
|
|
133 set(FLANG_BINARY_DIR ${CMAKE_BINARY_DIR}/tools/flang)
|
|
134 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
|
|
135 if (LINK_WITH_FIR)
|
|
136 set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
|
|
137 set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
|
|
138 set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
|
|
139 set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>)
|
|
140 include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
|
|
141 include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
|
|
142 endif()
|
|
143 endif()
|
|
144
|
|
145 if(LINK_WITH_FIR)
|
|
146 # tco tool and FIR lib output directories
|
|
147 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
|
|
148 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
|
|
149 # Always build tco tool
|
|
150 set(LLVM_BUILD_TOOLS ON)
|
|
151 message(STATUS "Linking driver with FIR and LLVM")
|
|
152 llvm_map_components_to_libnames(LLVM_COMMON_LIBS support)
|
|
153 message(STATUS "LLVM libraries: ${LLVM_COMMON_LIBS}")
|
|
154 endif()
|
|
155
|
|
156 # Add Flang-centric modules to cmake path.
|
|
157 include_directories(BEFORE
|
|
158 ${FLANG_BINARY_DIR}/include
|
|
159 ${FLANG_SOURCE_DIR}/include)
|
|
160
|
|
161 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
|
|
162
|
|
163 if (NOT DEFAULT_SYSROOT)
|
|
164 set(DEFAULT_SYSROOT "" CACHE PATH
|
|
165 "The <path> to use for the system root for all compiler invocations (--sysroot=<path>).")
|
|
166 endif()
|
|
167
|
|
168 if (NOT ENABLE_LINKER_BUILD_ID)
|
|
169 set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
|
|
170 endif()
|
|
171
|
|
172 set(FLANG_DEFAULT_LINKER "" CACHE STRING
|
|
173 "Default linker to use (linker name or absolute path, empty for platform default)")
|
|
174
|
|
175 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
|
|
176 "Default Fortran runtime library to use (\"libFortranRuntime\"), leave empty for platform default.")
|
|
177
|
|
178 if (NOT(FLANG_DEFAULT_RTLIB STREQUAL ""))
|
|
179 message(WARNING "Resetting Flang's default runtime library to use platform default.")
|
|
180 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
|
|
181 "Default runtime library to use (empty for platform default)" FORCE)
|
|
182 endif()
|
|
183
|
|
184
|
|
185
|
|
186 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
|
|
187 # Override LLVM versioning for now...
|
|
188 set(FLANG_VERSION_MAJOR "0")
|
|
189 set(FLANG_VERSION_MINOR "1")
|
|
190 set(FLANG_VERSION_PATCHLEVEL "0")
|
|
191
|
|
192
|
|
193 if (NOT DEFINED FLANG_VERSION_MAJOR)
|
|
194 set(FLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
|
|
195 endif()
|
|
196
|
|
197 if (NOT DEFINED FLANG_VERSION_MINOR)
|
|
198 set(FLANG_VERSION_MINOR ${LLVM_VERSION_MINOR})
|
|
199 endif()
|
|
200
|
|
201 if (NOT DEFINED FLANG_VERSION_PATCHLEVEL)
|
|
202 set(FLANG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
|
|
203 endif()
|
|
204
|
|
205 # Unlike PACKAGE_VERSION, FLANG_VERSION does not include LLVM_VERSION_SUFFIX.
|
|
206 set(FLANG_VERSION "${FLANG_VERSION_MAJOR}.${FLANG_VERSION_MINOR}.${FLANG_VERSION_PATCHLEVEL}")
|
|
207 message(STATUS "Flang version: ${FLANG_VERSION}")
|
|
208 # Flang executable version information
|
|
209 set(FLANG_EXECUTABLE_VERSION
|
|
210 "${FLANG_VERSION_MAJOR}" CACHE STRING
|
|
211 "Major version number to appended to the flang executable name.")
|
|
212 set(LIBFLANG_LIBRARY_VERSION
|
|
213 "${FLANG_VERSION_MAJOR}" CACHE STRING
|
|
214 "Major version number to appended to the libflang library.")
|
|
215
|
|
216 mark_as_advanced(FLANG_EXECUTABLE_VERSION LIBFLANG_LIBRARY_VERSION)
|
|
217
|
|
218 set(FLANG_VENDOR ${PACKAGE_VENDOR} CACHE STRING
|
|
219 "Vendor-specific Flang version information.")
|
|
220 set(FLANG_VENDOR_UTI "org.llvm.flang" CACHE STRING
|
|
221 "Vendor-specific uti.")
|
|
222
|
|
223 if (FLANG_VENDOR)
|
|
224 add_definitions(-DFLANG_VENDOR="${FLANG_VENDOR} ")
|
|
225 endif()
|
|
226
|
|
227 set(FLANG_REPOSITORY_STRING "" CACHE STRING
|
|
228 "Vendor-specific text for showing the repository the source is taken from.")
|
|
229 if (FLANG_REPOSITORY_STRING)
|
|
230 add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}")
|
|
231 endif()
|
|
232
|
|
233 # Configure Flang's Version.inc file.
|
|
234 configure_file(
|
|
235 ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in
|
|
236 ${CMAKE_CURRENT_BINARY_DIR}/include/flang/Version.inc)
|
|
237 # Configure Flang's version info header file.
|
|
238 configure_file(
|
|
239 ${FLANG_SOURCE_DIR}/include/flang/Config/config.h.cmake
|
|
240 ${FLANG_BINARY_DIR}/include/flang/Config/config.h)
|
|
241
|
|
242 # Add global F18 flags.
|
|
243 set(CMAKE_CXX_FLAGS "-fno-rtti -fno-exceptions -pedantic -Wall -Wextra -Werror -Wcast-qual -Wimplicit-fallthrough -Wdelete-non-virtual-dtor ${CMAKE_CXX_FLAGS}")
|
|
244
|
|
245 # Builtin check_cxx_compiler_flag doesn't seem to work correctly
|
|
246 macro(check_compiler_flag flag resultVar)
|
|
247 unset(${resultVar} CACHE)
|
|
248 check_cxx_compiler_flag("${flag}" ${resultVar})
|
|
249 endmacro()
|
|
250
|
|
251 check_compiler_flag("-Werror -Wno-deprecated-copy" CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
|
|
252 if (CXX_SUPPORTS_NO_DEPRECATED_COPY_FLAG)
|
|
253 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-copy")
|
|
254 endif()
|
|
255 check_compiler_flag("-Wstring-conversion" CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
|
|
256 if (CXX_SUPPORTS_NO_STRING_CONVERSION_FLAG)
|
|
257 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-string-conversion")
|
|
258 endif()
|
|
259
|
|
260 # Add appropriate flags for GCC
|
|
261 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
|
|
262
|
|
263 if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
|
264 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -fno-semantic-interposition")
|
|
265 else()
|
|
266 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument -Wstring-conversion \
|
|
267 -Wcovered-switch-default")
|
|
268 endif() # Clang.
|
|
269
|
|
270 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
|
|
271 if (CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
|
|
272 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types")
|
|
273 endif()
|
|
274
|
|
275 # Add to or adjust build type flags.
|
|
276 #
|
|
277 # TODO: This needs some extra thought. CMake's default for release builds
|
|
278 # is -O3, which can cause build failures on certain platforms (and compilers)
|
|
279 # with the current code base -- some templated functions are inlined and don't
|
|
280 # become available at link time when using -O3 (with Clang under MacOS/darwin).
|
|
281 # If we reset CMake's default flags we also clobber any user provided settings;
|
|
282 # make it difficult to customize a build in this regard... The setup below
|
|
283 # has this side effect but enables successful builds across multiple platforms
|
|
284 # in release mode...
|
|
285 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUGF18")
|
|
286 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -DCHECK=\"(void)\"") # do we need -O2 here?
|
|
287 set(CMAKE_CXX_FLAGS_RELEASE "-O2")
|
|
288
|
|
289 # Building shared libraries is bad for performance with GCC by default
|
|
290 # due to the need to preserve the right to override external entry points
|
|
291 if (BUILD_SHARED_LIBS AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
|
|
292 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-semantic-interposition")
|
|
293 endif()
|
|
294
|
|
295 endif()
|
|
296
|
|
297 list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS)
|
|
298
|
|
299 # Determine HOST_LINK_VERSION on Darwin.
|
|
300 set(HOST_LINK_VERSION)
|
|
301 if (APPLE)
|
|
302 set(LD_V_OUTPUT)
|
|
303 execute_process(
|
|
304 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
|
|
305 RESULT_VARIABLE HAD_ERROR
|
|
306 OUTPUT_VARIABLE LD_V_OUTPUT)
|
|
307 if (NOT HAD_ERROR)
|
|
308 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
|
|
309 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
|
|
310 elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
|
|
311 string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
|
|
312 endif()
|
|
313 else()
|
|
314 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
|
|
315 endif()
|
|
316 endif()
|
|
317
|
|
318 include(CMakeParseArguments)
|
|
319 include(AddFlang)
|
|
320
|
|
321
|
|
322 add_subdirectory(include)
|
|
323 add_subdirectory(lib)
|
|
324 add_subdirectory(cmake/modules)
|
|
325
|
|
326 option(FLANG_BUILD_TOOLS
|
|
327 "Build the Flang tools. If OFF, just generate build targets." ON)
|
|
328 if (FLANG_BUILD_TOOLS)
|
|
329 add_subdirectory(tools)
|
|
330 endif()
|
|
331 add_subdirectory(runtime)
|
|
332
|
|
333 if (FLANG_INCLUDE_TESTS)
|
|
334 enable_testing()
|
|
335 add_subdirectory(test)
|
|
336 add_subdirectory(unittests)
|
|
337 endif()
|
|
338
|
|
339 option(FLANG_INCLUDE_DOCS "Generate build targets for the Flang docs."
|
|
340 ${LLVM_INCLUDE_DOCS})
|
|
341 if (FLANG_INCLUDE_DOCS)
|
|
342 add_subdirectory(docs)
|
|
343 endif()
|
|
344
|
|
345 # Custom target to install Flang libraries.
|
|
346 add_custom_target(flang-libraries)
|
|
347 set_target_properties(flang-libraries PROPERTIES FOLDER "Misc")
|
|
348
|
|
349 if (NOT LLVM_ENABLE_IDE)
|
|
350 add_llvm_install_targets(install-flang-libraries
|
|
351 DEPENDS flang-libraries
|
|
352 COMPONENT flang-libraries)
|
|
353 endif()
|
|
354
|
|
355 get_property(FLANG_LIBS GLOBAL PROPERTY FLANG_LIBS)
|
|
356 if (FLANG_LIBS)
|
|
357 list(REMOVE_DUPLICATES FLANG_LIBS)
|
|
358 foreach(lib ${FLANG_LIBS})
|
|
359 add_dependencies(flang-libraries ${lib})
|
|
360 if (NOT LLVM_ENABLE_IDE)
|
|
361 add_dependencies(install-flang-libraries install-${lib})
|
|
362 endif()
|
|
363 endforeach()
|
|
364 endif()
|
|
365
|
|
366 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
|
|
367 install(DIRECTORY include/flang
|
|
368 DESTINATION include
|
|
369 COMPONENT flang-headers
|
|
370 FILES_MATCHING
|
|
371 PATTERN "*.def"
|
|
372 PATTERN "*.h"
|
|
373 PATTERN "*.inc"
|
|
374 PATTERN "*.td"
|
|
375 PATTERN "config.h" EXCLUDE
|
|
376 PATTERN ".git" EXCLUDE
|
|
377 PATTERN "CMakeFiles" EXCLUDE)
|
|
378 endif()
|