Skip to content

Commit 849b0b9

Browse files
committed
Misc CMake file fixes for compiling in Windows
- Call project command at the beginning if compiling flang in standalone mode This makes sure that CMake variables like CMAKE_SYSTEM_PROCESSOR are defined and removes the need for hacks like calling uname -m - Prevent CMake from running tests to check if flang works flang.exe is not fully functional and CMake tries to run checks on the Fortran compiler (more than in the Unix case) and fails. By setting CMake options like CMAKE_Fortran_COMPILER_SUPPORTS_F90, CMake accepts flang.exe as the Fortran compiler - Alias AMD64/amd64 to x86_64 - Alias arm64 to x86_64 - Preprocessor options like TARGET_LINUX are replaced with TARGET_${OS} - Allow building flang_shared and flang_static in parallel By setting two different directories for Fortran_MODULE_DIRECTORY they can now be built in parallel - Link to libm only on non-windows - Remove .so from name when linking When linking libomp.so is not portable. Use NAMES omp libomp for portability - Call built executables using the target name instead of full path The full path to the built executable given is not portable, but using the target name is portable as CMake will internally figure out the full path - Sort upperilm.in using portable CMake code
1 parent c0828b7 commit 849b0b9

File tree

10 files changed

+86
-72
lines changed

10 files changed

+86
-72
lines changed

CMakeLists.txt

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,56 @@
66

77
cmake_minimum_required(VERSION 3.3)
88

9+
# If we are not building as a part of LLVM, build Flang as a
10+
# standalone project, using LLVM as an external library:
11+
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
12+
project(Flang)
13+
endif()
14+
915
# In order to bootstrap the runtime library we need to skip
1016
# CMake's Fortran tests
1117
SET(CMAKE_Fortran_COMPILER_WORKS 1)
12-
13-
if( NOT DEFINED TARGET_ARCHITECTURE )
14-
execute_process(COMMAND uname -m OUTPUT_STRIP_TRAILING_WHITESPACE
15-
OUTPUT_VARIABLE TARGET_ARCHITECTURE)
18+
if (WIN32)
19+
SET(CMAKE_Fortran_ABI_COMPILED 0)
20+
SET(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
1621
endif()
1722

18-
if( NOT DEFINED TARGET_OS )
19-
execute_process(COMMAND uname -s OUTPUT_STRIP_TRAILING_WHITESPACE
20-
OUTPUT_VARIABLE TARGET_OS)
23+
set(TARGET_OS ${CMAKE_SYSTEM_NAME} CACHE STRING "Target OS")
24+
set(TARGET_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING "Target Architecture")
25+
26+
if (POLICY CMP0054)
27+
cmake_policy(SET CMP0054 NEW)
2128
endif()
2229

2330
if( ${TARGET_OS} STREQUAL "Linux" )
2431
set(OS "LINUX")
2532
set(OSNAME "Linux")
26-
if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
27-
set(ARCHNAME x86-64)
28-
set(ARCH X86)
29-
set(WRDSZ 64)
30-
elseif( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
31-
set(ARCHNAME aarch64)
32-
set(ARCH ARM)
33-
set(WRDSZ 64)
34-
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
35-
set(ARCHNAME ppc64le)
36-
set(ARCH POWER)
37-
set(WRDSZ 64)
38-
else()
39-
message("Unsupported architecture: ${TARGET_ARCHITECTURE}" )
40-
return()
41-
endif()
4233
else()
4334
message("Unsupported OS: ${TARGET_OS}" )
4435
return()
4536
endif()
4637

47-
# The cmake documentation states that these are set. They are not so we
48-
# set them here
49-
set(CMAKE_HOST_SYSTEM_NAME ${TARGET_OS})
50-
set(CMAKE_HOST_SYSTEM_PROCESSOR ${TARGET_ARCHITECTURE})
38+
if (${TARGET_ARCHITECTURE} MATCHES "^(x86_64|AMD64|amd64)$")
39+
set(TARGET_ARCHITECTURE x86_64)
40+
set(ARCHNAME x86-64)
41+
set(ARCH X86)
42+
elseif (${TARGET_ARCHITECTURE} MATCHES "^(aarch64|arm64)$")
43+
set(TARGET_ARCHITECTURE aarch64)
44+
set(ARCHNAME aarch64)
45+
set(ARCH ARM)
46+
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
47+
set(ARCHNAME ppc64le)
48+
set(ARCH POWER)
49+
else()
50+
message("Unsupported architecture: ${TARGET_ARCHITECTURE}" )
51+
return()
52+
endif()
53+
54+
math(EXPR WRDSZ "${CMAKE_SIZEOF_VOID_P} * 8")
5155

5256
# If we are not building as a part of LLVM, build Flang as an
5357
# standalone project, using LLVM as an external library:
5458
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
55-
project(Flang)
56-
5759
# Rely on llvm-config.
5860
set(CONFIG_OUTPUT)
5961

runtime/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ set (RUNTIME_SHARED_DIR ${CMAKE_CURRENT_SOURCE_DIR}/shared)
99
add_definitions(
1010
-DMAXCPUS=256
1111
-DMAXCPUSL=8
12-
-DMAXCPUSR=8
13-
-DTARGET_LINUX
14-
-DTARGET_LLVM
15-
-DLINUX
12+
-DMAXCPUSR=8
13+
-DTARGET_LLVM
1614
-DPGF90
1715
-DPGFLANG
1816
-DNATIVE_FPCVT
1917
-DPGI_LITTLE_ENDIAN
2018
-DINLINE_MEMOPS
19+
-DTARGET_${OS}
20+
-D${OS}
2121
)
2222

2323
if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
2424
add_definitions(
2525
-DTARGET_X8664
26-
-DTARGET_LINUX_X8664
26+
-DTARGET_${OS}_X8664
2727
)
2828
elseif( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
2929
add_definitions(
3030
-DTARGET_LLVM_ARM64
31-
-DTARGET_LINUX_ARM
31+
-DTARGET_${OS}_ARM
3232
)
3333
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
3434
add_definitions(
35-
-DTARGET_LINUX_POWER
36-
-DLINUX_POWER
35+
-DTARGET_${OS}_POWER
36+
-D${OS}_POWER
3737
)
3838
endif()
3939

runtime/flang/CMakeLists.txt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ enable_language(C ASM Fortran) # Enable assembly and Fortran
88

99
SET(ASM_OPTIONS "-DLINUX_ELF")
1010
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS}" )
11-
SET(CMAKE_SHARED_LINKER_FLAGS "-no-flang-libs")
11+
if (NOT MSVC)
12+
SET(CMAKE_SHARED_LINKER_FLAGS "-no-flang-libs")
13+
else ()
14+
SET(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-flang-libs")
15+
endif ()
1216

1317
# We are using Fortran driver to build this library with fresh compiler
1418
# components, so point its binary directory to the build directory to pick up
@@ -475,10 +479,14 @@ add_flang_library(flang_static
475479
${FTN_SUPPORT_I8}
476480
${SHARED_SOURCES}
477481
)
482+
set_target_properties(flang_static
483+
PROPERTIES
484+
Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include-static
485+
)
478486
if (MSVC)
479-
set_property(TARGET flang_static PROPERTY OUTPUT_NAME libflang)
487+
set_property(TARGET flang_static PROPERTY OUTPUT_NAME libflang)
480488
else()
481-
set_property(TARGET flang_static PROPERTY OUTPUT_NAME flang)
489+
set_property(TARGET flang_static PROPERTY OUTPUT_NAME flang)
482490
endif()
483491

484492
set(SHARED_LIBRARY TRUE)
@@ -492,18 +500,7 @@ add_flang_library(flang_shared
492500
${SHARED_SOURCES}
493501
)
494502
set_property(TARGET flang_shared PROPERTY OUTPUT_NAME flang)
495-
496-
#
497-
# Seralize the building of flang_shared and flang_static to eliminate
498-
# conflicts with the same module files from the shared and static builds
499-
# being created/recreated in the common directory
500-
# ${CMAKE_Fortran_MODULE_DIRECTORY}.
501-
#
502-
# Note: building of each library is still parallelized.
503-
#
504-
add_dependencies(flang_shared flang_static)
505-
506-
target_link_libraries(flang_shared ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/libflangrti.so)
503+
target_link_libraries(flang_shared flangrti_shared)
507504
# Resolve symbols against libm and librt
508505
if (NOT MSVC)
509506
target_link_libraries(flang_shared m rt)

runtime/flangrti/CMakeLists.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#
23
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
34
# See https://llvm.org/LICENSE.txt for license information.
@@ -75,28 +76,31 @@ add_flang_library(flangrti_shared
7576
)
7677

7778
# Resolve symbols against libm
78-
target_link_libraries(flangrti_shared m)
79+
if (NOT MSVC)
80+
target_link_libraries(flangrti_shared PRIVATE m)
81+
endif()
82+
7983

8084
# Resolve symbols against libpthread
8185
find_package(Threads REQUIRED)
8286
if (CMAKE_THREAD_LIBS_INIT)
83-
target_link_libraries(flangrti_shared "${CMAKE_THREAD_LIBS_INIT}")
87+
target_link_libraries(flangrti_shared PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
8488
endif()
8589

8690
# Import OpenMP
8791
if (NOT DEFINED LIBOMP_EXPORT_DIR)
8892
find_library(
8993
FLANG_LIBOMP
90-
libomp.so
94+
NAMES omp libomp
9195
HINTS ${CMAKE_BINARY_DIR}/lib)
92-
target_link_libraries(flangrti_shared ${FLANG_LIBOMP})
96+
target_link_libraries(flangrti_shared PRIVATE ${FLANG_LIBOMP})
9397
endif()
9498

9599
find_library(
96100
LIBPGMATH
97-
libpgmath.so
101+
NAMES pgmath libpgmath
98102
HINTS ${CMAKE_BINARY_DIR}/lib)
99-
target_link_libraries(flangrti_shared ${LIBPGMATH})
103+
target_link_libraries(flangrti_shared PRIVATE ${LIBPGMATH})
100104

101105
if( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
102106
target_compile_definitions(flangrti_static PRIVATE TARGET_LINUX_ARM)

tools/flang1/flang1exe/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,13 @@ target_compile_options(flang1
155155
target_link_libraries(flang1
156156
PRIVATE
157157
flangArgParser
158-
${FLANG_LIB_DIR}/scutil.a
159-
-lm
158+
scutil
160159
)
161160

161+
if (NOT MSVC)
162+
target_link_libraries(flang1 PRIVATE m)
163+
endif()
164+
162165
# Install flang1 executable
163166
install(TARGETS flang1
164167
RUNTIME DESTINATION bin)

tools/flang1/utils/symtab/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ add_custom_command(
1313
${UTILS_SYMTAB_BIN_DIR}/symtabdf.c
1414
${UTILS_SYMTAB_BIN_DIR}/symnames.h
1515
${FLANG1_DOC_BIN_DIR}/symtab.rst
16-
COMMAND ${CMAKE_BINARY_DIR}/bin/fesymutil ${CMAKE_CURRENT_SOURCE_DIR}/symtab.n
16+
COMMAND fesymutil ${CMAKE_CURRENT_SOURCE_DIR}/symtab.n
1717
${CMAKE_CURRENT_SOURCE_DIR}/symtab.in.h
1818
-o -n ${UTILS_SYMTAB_BIN_DIR}/symtab.out.n
1919
${UTILS_SYMTAB_BIN_DIR}/symtab.h
@@ -43,7 +43,7 @@ add_custom_command(
4343
${UTILS_SYMTAB_BIN_DIR}/astdf.d
4444
${UTILS_SYMTAB_BIN_DIR}/ilmtp.h
4545
${FLANG1_DOC_BIN_DIR}/symini.rst
46-
COMMAND ${CMAKE_BINARY_DIR}/bin/fesymini ${UTILS_SYMTAB_DIR}/symini_ftn.n
46+
COMMAND fesymini ${UTILS_SYMTAB_DIR}/symini_ftn.n
4747
-o ${UTILS_SYMTAB_BIN_DIR}/syminidf.h
4848
${UTILS_SYMTAB_BIN_DIR}/pd.h
4949
${UTILS_SYMTAB_BIN_DIR}/ast.d

tools/flang2/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ set(FLANG2_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
2323

2424
include_directories(${FLANG2_INCLUDE_DIR})
2525

26-
if( ${TARGET_OS} STREQUAL "Linux" )
27-
if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
28-
set(X86_64 ON)
29-
set(LINUX86 ON)
30-
endif()
26+
if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
27+
set(X86_64 ON)
3128
endif()
3229

3330
option(FLANG_OPENMP_GPU_NVIDIA "Enable OpenMP Accelerator Offload." OFF)

tools/flang2/flang2exe/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ target_link_libraries(flang2
143143
scutil
144144
)
145145

146+
if (NOT MSVC)
147+
target_link_libraries(flang2 PRIVATE m)
148+
endif()
149+
146150
add_dependencies(flang2
147151
gen_backend_error_headers # Error message headers
148152
gen_backend_symtab # Symbol table headers

tools/flang2/include/platform.h.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* - true for all arch/arch
1616
*/
1717
#define BIGOBJ 1
18-
#cmakedefine LINUX86 1
1918
#define LONG_IS_64 1
2019
#define NOLZ 1
2120
#cmakedefine X86_64 1

tools/flang2/utils/upper/CMakeLists.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
#
66

7-
set(ENV{LC_ALL} "C")
8-
97
add_executable(upperl
108
upperl.c
119
)
1210

1311
# Generate upper tables
12+
file(STRINGS "${UTILS_UPPER_DIR}/upperilm.in" UPPERILM_H_CONTENTS)
13+
list(SORT UPPERILM_H_CONTENTS)
14+
set(UPPERILM_H_CONTENTS_SORTED "")
15+
foreach(Line ${UPPERILM_H_CONTENTS})
16+
# Don't modify the line if it contains #local at the end.
17+
string(SUBSTRING "${Line}" 0 1 FIRST_CHAR)
18+
if(NOT "${FIRST_CHAR}" STREQUAL "#")
19+
set(UPPERILM_H_CONTENTS_SORTED "${UPPERILM_H_CONTENTS_SORTED}${Line}\n")
20+
endif()
21+
endforeach()
22+
file(WRITE ${UTILS_UPPER_BIN_DIR}/upperilm.sort "${UPPERILM_H_CONTENTS_SORTED}")
1423

1524
add_custom_command(
1625
OUTPUT ${UTILS_UPPER_BIN_DIR}/upperilm.h
17-
COMMAND LC_ALL=C sort ${UTILS_UPPER_DIR}/upperilm.in | grep -v "^ *\#" > ${UTILS_UPPER_BIN_DIR}/upperilm.sort
18-
COMMAND ${CMAKE_BINARY_DIR}/bin/upperl ${UTILS_UPPER_BIN_DIR}/upperilm.sort ${UTILS_UPPER_BIN_DIR}/upperilm.h
26+
COMMAND upperl ${UTILS_UPPER_BIN_DIR}/upperilm.sort ${UTILS_UPPER_BIN_DIR}/upperilm.h
1927
DEPENDS upperl ${UTILS_UPPER_DIR}/upperilm.in
2028
)
2129

0 commit comments

Comments
 (0)