Skip to content

Commit 6c16aa4

Browse files
pmccormickDavidTruby
authored andcommitted
[flang] A rework of the cmake build components for in and out of tree builds.
In general all the basic functionality seems to work and removes some redundancy and more complicated features in favor of borrowing infrastructure from LLVM build configurations. Here's a quick summary of details and remaining issues: * Testing has spanned Ubuntu 18.04 & 19.10, CentOS 7, RHEL 8, and MacOS/darwin. Architectures include x86_64 and Arm. Without access to Window nothing has been tested there yet. * As we change file and directory naming schemes (i.e., capitalization) some odd things can occur on MacOS systems with case preserving but not case senstive file system configurations. Can be painful and certainly something to watch out for as any any such changes continue. * Testing infrastructure still needs to be tuned up and worked on. Note that there do appear to be cases of some tests hanging (on MacOS in particular). They appear unrelated to the build process. * Shared library configurations need testing (and probably fixing). * Tested both standalone and 'in-mono repo' builds. Changes for supporting the mono repo builds will require LLVM-level changes that are straightforward when the time comes. * The configuration contains a work-around for LLVM's C++ standard mode passing down into Flang/F18 builds (i.e., LLVM CMake configuration would force a -std=c++11 flag to show up in command line arguments. The current configuration removes that automatically and is more strict in following new CMake guidelines for enforcing C++17 mode across all the CMake files. * Cleaned up a lot of repetition in the command line arguments. It is likely that more work is still needed to both allow for customization and working around CMake defailts (or those inherited from LLVM's configuration files). On some platforms agressive optimization flags (e.g. -O3) can actually break builds due to the inlining of templates in .cpp source files that then no longer are available for use cases outside those source files (shows up as link errors). Sticking at -O2 appears to fix this. Currently this CMake configuration forces this in release mode but at the cost of stomping on any CMake, or user customized, settings for the release flags. * Made the lit tests non-source directory dependent where appropriate. This is done by configuring certain test shell files to refer to the correct paths whether an in or out of tree build is being performed. These configured files are output in the build directory. A %B substitution is introduced in lit to refer to the build directory, mirroring the %S substitution for the source directory, so that the tests can refer to the configured shell scripts. Co-authored-by: David Truby <david.truby@arm.com> Original-commit: flang-compiler/f18@d1c7184 Reviewed-on: flang-compiler/f18#1045
1 parent 53d5d9f commit 6c16aa4

File tree

246 files changed

+857
-496
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

246 files changed

+857
-496
lines changed

flang/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ CMakeCache.txt
1717
*/*/Makefile
1818
cmake_install.cmake
1919
formatted
20+
.DS_Store
21+
.vs_code

flang/CMakeLists.txt

+328-120
Large diffs are not rendered by default.

flang/README.md

-25
Original file line numberDiff line numberDiff line change
@@ -150,43 +150,18 @@ or
150150
```
151151
CXX=/opt/gcc-7.2/bin/g++-7.2 cmake ...
152152
```
153-
There's a third option!
154-
The CMakeList.txt file uses the variable GCC
155-
as the path to the bin directory containing the C++ compiler.
156-
157-
GCC can be defined on the cmake command line
158-
where `<GCC_DIRECTORY>` is the path to a GCC installation with bin, lib, etc:
159-
```
160-
cmake -DGCC=<GCC_DIRECTORY> ...
161-
```
162153

163154
### Building f18 with clang
164155

165156
To build f18 with clang,
166157
cmake needs to know how to find clang++
167158
and the GCC library and tools that were used to build clang++.
168159

169-
The CMakeList.txt file expects either CXX or BUILD_WITH_CLANG to be set.
170-
171160
CXX should include the full path to clang++
172161
or clang++ should be found on your PATH.
173162
```
174163
export CXX=clang++
175164
```
176-
BUILD_WITH_CLANG can be defined on the cmake command line
177-
where `<CLANG_DIRECTORY>`
178-
is the path to a clang installation with bin, lib, etc:
179-
```
180-
cmake -DBUILD_WITH_CLANG=<CLANG_DIRECTORY>
181-
```
182-
Or GCC can be defined on the f18 cmake command line
183-
where `<GCC_DIRECTORY>` is the path to a GCC installation with bin, lib, etc:
184-
```
185-
cmake -DGCC=<GCC_DIRECTORY> ...
186-
```
187-
To use f18 after it is built,
188-
the environment variables PATH and LD_LIBRARY_PATH
189-
must be set to use GCC and its associated libraries.
190165

191166
### Installation Directory
192167

flang/cmake/modules/AddFlang.cmake

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
macro(set_flang_windows_version_resource_properties name)
2+
if (DEFINED windows_resource_file)
3+
set_windows_version_resource_properties(${name} ${windows_resource_file}
4+
VERSION_MAJOR ${FLANG_VERSION_MAJOR}
5+
VERSION_MINOR ${FLANG_VERSION_MINOR}
6+
VERSION_PATCHLEVEL ${FLANG_VERSION_PATCHLEVEL}
7+
VERSION_STRING "${FLANG_VERSION} (${BACKEND_PACKAGE_STRING})"
8+
PRODUCT_NAME "flang")
9+
endif()
10+
endmacro()
11+
12+
macro(add_flang_subdirectory name)
13+
add_llvm_subdirectory(FLANG TOOL ${name})
14+
endmacro()
15+
16+
macro(add_flang_library name)
17+
cmake_parse_arguments(ARG
18+
"SHARED"
19+
""
20+
"ADDITIONAL_HEADERS"
21+
${ARGN})
22+
set(srcs)
23+
if (MSVC_IDE OR XCODE)
24+
# Add public headers
25+
file(RELATIVE_PATH lib_path
26+
${FLANG_SOURCE_DIR}/lib/
27+
${CMAKE_CURRENT_SOURCE_DIR})
28+
if(NOT lib_path MATCHES "^[.][.]")
29+
file( GLOB_RECURSE headers
30+
${FLANG_SOURCE_DIR}/include/flang/${lib_path}/*.h
31+
${FLANG_SOURCE_DIR}/include/flang/${lib_path}/*.def)
32+
set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
33+
34+
if (headers)
35+
set(srcs ${headers})
36+
endif()
37+
endif()
38+
endif(MSVC_IDE OR XCODE)
39+
40+
if (srcs OR ARG_ADDITIONAL_HEADERS)
41+
set(srcs
42+
ADDITIONAL_HEADERS
43+
${srcs}
44+
${ARG_ADDITIONAL_HEADERS}) # It may contain unparsed unknown args.
45+
46+
endif()
47+
48+
if (ARG_SHARED)
49+
set(LIBTYPE SHARED)
50+
else()
51+
# llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
52+
# so we need to handle it here.
53+
if (BUILD_SHARED_LIBS)
54+
set(LIBTYPE SHARED OBJECT)
55+
else()
56+
set(LIBTYPE STATIC OBJECT)
57+
endif()
58+
set_property(GLOBAL APPEND PROPERTY FLANG_STATIC_LIBS ${name})
59+
endif()
60+
61+
llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
62+
63+
if (TARGET ${name})
64+
target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})
65+
66+
if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libflang")
67+
set(export_to_flangtargets)
68+
if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
69+
"flang-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
70+
NOT LLVM_DISTRIBUTION_COMPONENTS)
71+
set(export_to_flangtargets EXPORT FlangTargets)
72+
set_property(GLOBAL PROPERTY FLANG_HAS_EXPORTS True)
73+
endif()
74+
75+
install(TARGETS ${name}
76+
COMPONENT ${name}
77+
${export_to_flangtargets}
78+
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
79+
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
80+
RUNTIME DESTINATION bin)
81+
82+
if (NOT LLVM_ENABLE_IDE)
83+
add_llvm_install_targets(install-${name}
84+
DEPENDS ${name}
85+
COMPONENT ${name})
86+
endif()
87+
88+
set_property(GLOBAL APPEND PROPERTY FLANG_LIBS ${name})
89+
endif()
90+
set_property(GLOBAL APPEND PROPERTY FLANG_EXPORTS ${name})
91+
else()
92+
# Add empty "phony" target
93+
add_custom_target(${name})
94+
endif()
95+
96+
set_target_properties(${name} PROPERTIES FOLDER "Flang libraries")
97+
set_flang_windows_version_resource_properties(${name})
98+
endmacro(add_flang_library)
99+
100+
macro(add_flang_executable name)
101+
add_llvm_executable(${name} ${ARGN})
102+
set_target_properties(${name} PROPERTIES FOLDER "Flang executables")
103+
set_flang_windows_version_resource_properties(${name})
104+
endmacro(add_flang_executable)
105+
106+
macro(add_flang_tool name)
107+
if (NOT FLANG_BUILD_TOOLS)
108+
set(EXCLUDE_FROM_ALL ON)
109+
endif()
110+
111+
add_flang_executable(${name} ${ARGN})
112+
add_dependencies(${name} flang-resource-headers)
113+
114+
if (FLANG_BUILD_TOOLS)
115+
set(export_to_flangtargets)
116+
if (${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
117+
NOT LLVM_DISTRIBUTION_COMPONENTS)
118+
set(export_to_flangtargets EXPORT FlangTargets)
119+
set_property(GLOBAL PROPERTY FLANG_HAS_EXPORTS True)
120+
endif()
121+
122+
install(TARGETS ${name}
123+
${export_to_flangtargets}
124+
RUNTIME DESTINATION bin
125+
COMPONENT ${name})
126+
127+
if(NOT LLVM_ENABLE_IDE)
128+
add_llvm_install_targets(install-${name}
129+
DEPENDS ${name}
130+
COMPONENT ${name})
131+
endif()
132+
set_property(GLOBAL APPEND PROPERTY FLANG_EXPORTS ${name})
133+
endif()
134+
endmacro()
135+
136+
macro(add_flang_symlink name dest)
137+
add_llvm_tool_symlink(${name} ${dest} ALWAYS_GENERATE)
138+
# Always generate install targets
139+
llvm_install_symlink(${name} ${dest} ALWAYS_GENERATE)
140+
endmacro()
141+

flang/cmake/modules/CMakeLists.txt

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Generate a list of CMake library targets so that other CMake projects can
2+
# link against them. LLVM calls its version of this file LLVMExports.cmake, but
3+
# the usual CMake convention seems to be ${Project}Targets.cmake.
4+
set(FLANG_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/flang)
5+
set(flang_cmake_builddir "${CMAKE_BINARY_DIR}/${FLANG_INSTALL_PACKAGE_DIR}")
6+
7+
# Keep this in sync with llvm/cmake/CMakeLists.txt!
8+
set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm)
9+
set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}")
10+
11+
get_property(FLANG_EXPORTS GLOBAL PROPERTY FLANG_EXPORTS)
12+
export(TARGETS ${FLANG_EXPORTS} FILE ${flang_cmake_builddir}/FlangTargets.cmake)
13+
14+
# Generate FlangConfig.cmake for the build tree.
15+
set(FLANG_CONFIG_CMAKE_DIR "${flang_cmake_builddir}")
16+
set(FLANG_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}")
17+
set(FLANG_CONFIG_EXPORTS_FILE "${flang_cmake_builddir}/FlangTargets.cmake")
18+
set(FLANG_CONFIG_INCLUDE_DIRS
19+
"${FLANG_SOURCE_DIR}/include"
20+
"${FLANG_BINARY_DIR}/include"
21+
)
22+
configure_file(
23+
${CMAKE_CURRENT_SOURCE_DIR}/FlangConfig.cmake.in
24+
${flang_cmake_builddir}/FlangConfig.cmake
25+
@ONLY)
26+
set(FLANG_CONFIG_CMAKE_DIR)
27+
set(FLANG_CONFIG_LLVM_CMAKE_DIR)
28+
set(FLANG_CONFIG_EXPORTS_FILE)
29+
30+
# Generate FlangConfig.cmake for the install tree.
31+
set(FLANG_CONFIG_CODE "
32+
# Compute the installation prefix from this LLVMConfig.cmake file location.
33+
get_filename_component(FLANG_INSTALL_PREFIX \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)")
34+
# Construct the proper number of get_filename_component(... PATH)
35+
# calls to compute the installation prefix.
36+
string(REGEX REPLACE "/" ";" _count "${FLANG_INSTALL_PACKAGE_DIR}")
37+
foreach(p ${_count})
38+
set(FLANG_CONFIG_CODE "${FLANG_CONFIG_CODE}
39+
get_filename_component(FLANG_INSTALL_PREFIX \"\${FLANG_INSTALL_PREFIX}\" PATH)")
40+
endforeach(p)
41+
42+
set(FLANG_CONFIG_CMAKE_DIR "\${FLANG_INSTALL_PREFIX}/${FLANG_INSTALL_PACKAGE_DIR}")
43+
set(FLANG_CONFIG_LLVM_CMAKE_DIR "\${FLANG_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}")
44+
set(FLANG_CONFIG_EXPORTS_FILE "\${FLANG_CMAKE_DIR}/FlangTargets.cmake")
45+
set(FLANG_CONFIG_INCLUDE_DIRS "\${FLANG_INSTALL_PREFIX}/include")
46+
47+
configure_file(
48+
${CMAKE_CURRENT_SOURCE_DIR}/FlangConfig.cmake.in
49+
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlangConfig.cmake
50+
@ONLY)
51+
52+
set(FLANG_CONFIG_CODE)
53+
set(FLANG_CONFIG_CMAKE_DIR)
54+
set(FLANG_CONFIG_EXPORTS_FILE)
55+
56+
if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
57+
get_property(flang_has_exports GLOBAL PROPERTY FLANG_HAS_EXPORTS)
58+
if(flang_has_exports)
59+
install(EXPORT FlangTargets DESTINATION ${FLANG_INSTALL_PACKAGE_DIR}
60+
COMPONENT flang-cmake-exports)
61+
endif()
62+
63+
install(FILES
64+
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlangConfig.cmake
65+
DESTINATION ${FLANG_INSTALL_PACKAGE_DIR}
66+
COMPONENT flang-cmake-exports)
67+
68+
if(NOT LLVM_ENABLE_IDE)
69+
# Add a dummy target so this can be used with LLVM_DISTRIBUTION_COMPONENTS
70+
add_custom_target(flang-cmake-exports)
71+
add_llvm_install_targets(install-flang-cmake-exports
72+
COMPONENT flang-cmake-exports)
73+
endif()
74+
endif()
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This file allows users to call find_package(Flang) and pick up our targets.
2+
3+
@FLANG_CONFIG_CODE@
4+
5+
find_package(LLVM REQUIRED CONFIG
6+
HINTS "@FLANG_CONFIG_LLVM_CMAKE_DIR@")
7+
8+
set(FLANG_EXPORTED_TARGETS "@FLANG_EXPORTS@")
9+
set(FLANG_CMAKE_DIR "FLANG_CONFIG_CMAKE_DIR@")
10+
set(FLANG_INCLUDE_DIRS "@FLANG_CONFIG_INCLUDE_DIRS@")
11+
12+
# Provide all our library targets to users.
13+
include("@FLANG_CONFIG_EXPORTS_FILE@")

flang/include/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(flang)

flang/include/flang/Version.inc.in

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define FLANG_VERSION @FLANG_VERSION@
2+
#define FLANG_VERSION_STRING "@FLANG_VERSION@"
3+
#define FLANG_VERSION_MAJOR @FLANG_VERSION_MAJOR@
4+
#define FLANG_VERSION_MINOR @FLANG_VERSION_MINOR@
5+
#define FLANG_VERSION_PATCHLEVEL @FLANG_VERSION_PATCHLEVEL@

flang/lib/CMakeLists.txt

-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
#===-- lib/CMakeLists.txt --------------------------------------------------===#
2-
#
3-
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4-
# See https://llvm.org/LICENSE.txt for license information.
5-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
#
7-
#===------------------------------------------------------------------------===#
8-
91
add_subdirectory(Common)
102
add_subdirectory(Evaluate)
113
add_subdirectory(Decimal)

flang/lib/Common/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#===-- lib/Common/CMakeLists.txt -------------------------------------------===#
2-
#
3-
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4-
# See https://llvm.org/LICENSE.txt for license information.
5-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
#
7-
#===------------------------------------------------------------------------===#
81

92
add_library(FortranCommon
103
Fortran.cpp
@@ -13,6 +6,8 @@ add_library(FortranCommon
136
idioms.cpp
147
)
158

9+
target_compile_features(FortranCommon PUBLIC cxx_std_17)
10+
1611
install (TARGETS FortranCommon
1712
ARCHIVE DESTINATION lib
1813
LIBRARY DESTINATION lib

flang/lib/Decimal/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
#===-- lib/Decimal/CMakeLists.txt ------------------------------------------===#
2-
#
3-
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4-
# See https://llvm.org/LICENSE.txt for license information.
5-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
#
7-
#===------------------------------------------------------------------------===#
81

92
add_library(FortranDecimal
103
binary-to-decimal.cpp
114
decimal-to-binary.cpp
125
)
136

7+
target_compile_features(FortranDecimal PUBLIC cxx_std_17)
8+
149
install (TARGETS FortranDecimal
1510
ARCHIVE DESTINATION lib
1611
LIBRARY DESTINATION lib

flang/lib/Evaluate/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#===-- lib/Evaluate/CMakeLists.txt -----------------------------------------===#
2-
#
3-
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4-
# See https://llvm.org/LICENSE.txt for license information.
5-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
#
7-
#===------------------------------------------------------------------------===#
81

92
add_library(FortranEvaluate
103
call.cpp
@@ -34,6 +27,8 @@ add_library(FortranEvaluate
3427
variable.cpp
3528
)
3629

30+
target_compile_features(FortranEvaluate PUBLIC cxx_std_17)
31+
3732
target_link_libraries(FortranEvaluate
3833
FortranCommon
3934
FortranDecimal

flang/lib/Parser/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#===-- lib/Parser/CMakeLists.txt -------------------------------------------===#
2-
#
3-
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4-
# See https://llvm.org/LICENSE.txt for license information.
5-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
#
7-
#===------------------------------------------------------------------------===#
81

92
add_library(FortranParser
103
Fortran-parsers.cpp
@@ -32,6 +25,8 @@ add_library(FortranParser
3225
user-state.cpp
3326
)
3427

28+
target_compile_features(FortranParser PRIVATE cxx_std_17)
29+
3530
target_link_libraries(FortranParser
3631
FortranCommon
3732
LLVMSupport

flang/lib/Semantics/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#===-- lib/Semantics/CMakeLists.txt ----------------------------------------===#
2-
#
3-
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4-
# See https://llvm.org/LICENSE.txt for license information.
5-
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
#
7-
#===------------------------------------------------------------------------===#
81

92
add_library(FortranSemantics
103
assignment.cpp
@@ -43,6 +36,8 @@ add_library(FortranSemantics
4336
unparse-with-symbols.cpp
4437
)
4538

39+
target_compile_features(FortranSemantics PUBLIC cxx_std_17)
40+
4641
target_link_libraries(FortranSemantics
4742
FortranCommon
4843
FortranEvaluate

0 commit comments

Comments
 (0)