Skip to content

Commit

Permalink
Misc CMake file fixes for compiling in Windows
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
isuruf committed Aug 4, 2021
1 parent c0828b7 commit 849b0b9
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 72 deletions.
60 changes: 31 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,56 @@

cmake_minimum_required(VERSION 3.3)

# If we are not building as a part of LLVM, build Flang as a
# standalone project, using LLVM as an external library:
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
project(Flang)
endif()

# In order to bootstrap the runtime library we need to skip
# CMake's Fortran tests
SET(CMAKE_Fortran_COMPILER_WORKS 1)

if( NOT DEFINED TARGET_ARCHITECTURE )
execute_process(COMMAND uname -m OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE TARGET_ARCHITECTURE)
if (WIN32)
SET(CMAKE_Fortran_ABI_COMPILED 0)
SET(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
endif()

if( NOT DEFINED TARGET_OS )
execute_process(COMMAND uname -s OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE TARGET_OS)
set(TARGET_OS ${CMAKE_SYSTEM_NAME} CACHE STRING "Target OS")
set(TARGET_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING "Target Architecture")

if (POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()

if( ${TARGET_OS} STREQUAL "Linux" )
set(OS "LINUX")
set(OSNAME "Linux")
if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
set(ARCHNAME x86-64)
set(ARCH X86)
set(WRDSZ 64)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
set(ARCHNAME aarch64)
set(ARCH ARM)
set(WRDSZ 64)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
set(ARCHNAME ppc64le)
set(ARCH POWER)
set(WRDSZ 64)
else()
message("Unsupported architecture: ${TARGET_ARCHITECTURE}" )
return()
endif()
else()
message("Unsupported OS: ${TARGET_OS}" )
return()
endif()

# The cmake documentation states that these are set. They are not so we
# set them here
set(CMAKE_HOST_SYSTEM_NAME ${TARGET_OS})
set(CMAKE_HOST_SYSTEM_PROCESSOR ${TARGET_ARCHITECTURE})
if (${TARGET_ARCHITECTURE} MATCHES "^(x86_64|AMD64|amd64)$")
set(TARGET_ARCHITECTURE x86_64)
set(ARCHNAME x86-64)
set(ARCH X86)
elseif (${TARGET_ARCHITECTURE} MATCHES "^(aarch64|arm64)$")
set(TARGET_ARCHITECTURE aarch64)
set(ARCHNAME aarch64)
set(ARCH ARM)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
set(ARCHNAME ppc64le)
set(ARCH POWER)
else()
message("Unsupported architecture: ${TARGET_ARCHITECTURE}" )
return()
endif()

math(EXPR WRDSZ "${CMAKE_SIZEOF_VOID_P} * 8")

# If we are not building as a part of LLVM, build Flang as an
# standalone project, using LLVM as an external library:
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
project(Flang)

# Rely on llvm-config.
set(CONFIG_OUTPUT)

Expand Down
16 changes: 8 additions & 8 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ set (RUNTIME_SHARED_DIR ${CMAKE_CURRENT_SOURCE_DIR}/shared)
add_definitions(
-DMAXCPUS=256
-DMAXCPUSL=8
-DMAXCPUSR=8
-DTARGET_LINUX
-DTARGET_LLVM
-DLINUX
-DMAXCPUSR=8
-DTARGET_LLVM
-DPGF90
-DPGFLANG
-DNATIVE_FPCVT
-DPGI_LITTLE_ENDIAN
-DINLINE_MEMOPS
-DTARGET_${OS}
-D${OS}
)

if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
add_definitions(
-DTARGET_X8664
-DTARGET_LINUX_X8664
-DTARGET_${OS}_X8664
)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
add_definitions(
-DTARGET_LLVM_ARM64
-DTARGET_LINUX_ARM
-DTARGET_${OS}_ARM
)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
add_definitions(
-DTARGET_LINUX_POWER
-DLINUX_POWER
-DTARGET_${OS}_POWER
-D${OS}_POWER
)
endif()

Expand Down
27 changes: 12 additions & 15 deletions runtime/flang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ enable_language(C ASM Fortran) # Enable assembly and Fortran

SET(ASM_OPTIONS "-DLINUX_ELF")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS}" )
SET(CMAKE_SHARED_LINKER_FLAGS "-no-flang-libs")
if (NOT MSVC)
SET(CMAKE_SHARED_LINKER_FLAGS "-no-flang-libs")
else ()
SET(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -no-flang-libs")
endif ()

# We are using Fortran driver to build this library with fresh compiler
# components, so point its binary directory to the build directory to pick up
Expand Down Expand Up @@ -475,10 +479,14 @@ add_flang_library(flang_static
${FTN_SUPPORT_I8}
${SHARED_SOURCES}
)
set_target_properties(flang_static
PROPERTIES
Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include-static
)
if (MSVC)
set_property(TARGET flang_static PROPERTY OUTPUT_NAME libflang)
set_property(TARGET flang_static PROPERTY OUTPUT_NAME libflang)
else()
set_property(TARGET flang_static PROPERTY OUTPUT_NAME flang)
set_property(TARGET flang_static PROPERTY OUTPUT_NAME flang)
endif()

set(SHARED_LIBRARY TRUE)
Expand All @@ -492,18 +500,7 @@ add_flang_library(flang_shared
${SHARED_SOURCES}
)
set_property(TARGET flang_shared PROPERTY OUTPUT_NAME flang)

#
# Seralize the building of flang_shared and flang_static to eliminate
# conflicts with the same module files from the shared and static builds
# being created/recreated in the common directory
# ${CMAKE_Fortran_MODULE_DIRECTORY}.
#
# Note: building of each library is still parallelized.
#
add_dependencies(flang_shared flang_static)

target_link_libraries(flang_shared ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/libflangrti.so)
target_link_libraries(flang_shared flangrti_shared)
# Resolve symbols against libm and librt
if (NOT MSVC)
target_link_libraries(flang_shared m rt)
Expand Down
16 changes: 10 additions & 6 deletions runtime/flangrti/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
Expand Down Expand Up @@ -75,28 +76,31 @@ add_flang_library(flangrti_shared
)

# Resolve symbols against libm
target_link_libraries(flangrti_shared m)
if (NOT MSVC)
target_link_libraries(flangrti_shared PRIVATE m)
endif()


# Resolve symbols against libpthread
find_package(Threads REQUIRED)
if (CMAKE_THREAD_LIBS_INIT)
target_link_libraries(flangrti_shared "${CMAKE_THREAD_LIBS_INIT}")
target_link_libraries(flangrti_shared PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
endif()

# Import OpenMP
if (NOT DEFINED LIBOMP_EXPORT_DIR)
find_library(
FLANG_LIBOMP
libomp.so
NAMES omp libomp
HINTS ${CMAKE_BINARY_DIR}/lib)
target_link_libraries(flangrti_shared ${FLANG_LIBOMP})
target_link_libraries(flangrti_shared PRIVATE ${FLANG_LIBOMP})
endif()

find_library(
LIBPGMATH
libpgmath.so
NAMES pgmath libpgmath
HINTS ${CMAKE_BINARY_DIR}/lib)
target_link_libraries(flangrti_shared ${LIBPGMATH})
target_link_libraries(flangrti_shared PRIVATE ${LIBPGMATH})

if( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
target_compile_definitions(flangrti_static PRIVATE TARGET_LINUX_ARM)
Expand Down
7 changes: 5 additions & 2 deletions tools/flang1/flang1exe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,13 @@ target_compile_options(flang1
target_link_libraries(flang1
PRIVATE
flangArgParser
${FLANG_LIB_DIR}/scutil.a
-lm
scutil
)

if (NOT MSVC)
target_link_libraries(flang1 PRIVATE m)
endif()

# Install flang1 executable
install(TARGETS flang1
RUNTIME DESTINATION bin)
Expand Down
4 changes: 2 additions & 2 deletions tools/flang1/utils/symtab/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ add_custom_command(
${UTILS_SYMTAB_BIN_DIR}/symtabdf.c
${UTILS_SYMTAB_BIN_DIR}/symnames.h
${FLANG1_DOC_BIN_DIR}/symtab.rst
COMMAND ${CMAKE_BINARY_DIR}/bin/fesymutil ${CMAKE_CURRENT_SOURCE_DIR}/symtab.n
COMMAND fesymutil ${CMAKE_CURRENT_SOURCE_DIR}/symtab.n
${CMAKE_CURRENT_SOURCE_DIR}/symtab.in.h
-o -n ${UTILS_SYMTAB_BIN_DIR}/symtab.out.n
${UTILS_SYMTAB_BIN_DIR}/symtab.h
Expand Down Expand Up @@ -43,7 +43,7 @@ add_custom_command(
${UTILS_SYMTAB_BIN_DIR}/astdf.d
${UTILS_SYMTAB_BIN_DIR}/ilmtp.h
${FLANG1_DOC_BIN_DIR}/symini.rst
COMMAND ${CMAKE_BINARY_DIR}/bin/fesymini ${UTILS_SYMTAB_DIR}/symini_ftn.n
COMMAND fesymini ${UTILS_SYMTAB_DIR}/symini_ftn.n
-o ${UTILS_SYMTAB_BIN_DIR}/syminidf.h
${UTILS_SYMTAB_BIN_DIR}/pd.h
${UTILS_SYMTAB_BIN_DIR}/ast.d
Expand Down
7 changes: 2 additions & 5 deletions tools/flang2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ set(FLANG2_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)

include_directories(${FLANG2_INCLUDE_DIR})

if( ${TARGET_OS} STREQUAL "Linux" )
if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
set(X86_64 ON)
set(LINUX86 ON)
endif()
if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
set(X86_64 ON)
endif()

option(FLANG_OPENMP_GPU_NVIDIA "Enable OpenMP Accelerator Offload." OFF)
Expand Down
4 changes: 4 additions & 0 deletions tools/flang2/flang2exe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ target_link_libraries(flang2
scutil
)

if (NOT MSVC)
target_link_libraries(flang2 PRIVATE m)
endif()

add_dependencies(flang2
gen_backend_error_headers # Error message headers
gen_backend_symtab # Symbol table headers
Expand Down
1 change: 0 additions & 1 deletion tools/flang2/include/platform.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* - true for all arch/arch
*/
#define BIGOBJ 1
#cmakedefine LINUX86 1
#define LONG_IS_64 1
#define NOLZ 1
#cmakedefine X86_64 1
Expand Down
16 changes: 12 additions & 4 deletions tools/flang2/utils/upper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

set(ENV{LC_ALL} "C")

add_executable(upperl
upperl.c
)

# Generate upper tables
file(STRINGS "${UTILS_UPPER_DIR}/upperilm.in" UPPERILM_H_CONTENTS)
list(SORT UPPERILM_H_CONTENTS)
set(UPPERILM_H_CONTENTS_SORTED "")
foreach(Line ${UPPERILM_H_CONTENTS})
# Don't modify the line if it contains #local at the end.
string(SUBSTRING "${Line}" 0 1 FIRST_CHAR)
if(NOT "${FIRST_CHAR}" STREQUAL "#")
set(UPPERILM_H_CONTENTS_SORTED "${UPPERILM_H_CONTENTS_SORTED}${Line}\n")
endif()
endforeach()
file(WRITE ${UTILS_UPPER_BIN_DIR}/upperilm.sort "${UPPERILM_H_CONTENTS_SORTED}")

add_custom_command(
OUTPUT ${UTILS_UPPER_BIN_DIR}/upperilm.h
COMMAND LC_ALL=C sort ${UTILS_UPPER_DIR}/upperilm.in | grep -v "^ *\#" > ${UTILS_UPPER_BIN_DIR}/upperilm.sort
COMMAND ${CMAKE_BINARY_DIR}/bin/upperl ${UTILS_UPPER_BIN_DIR}/upperilm.sort ${UTILS_UPPER_BIN_DIR}/upperilm.h
COMMAND upperl ${UTILS_UPPER_BIN_DIR}/upperilm.sort ${UTILS_UPPER_BIN_DIR}/upperilm.h
DEPENDS upperl ${UTILS_UPPER_DIR}/upperilm.in
)

Expand Down

0 comments on commit 849b0b9

Please sign in to comment.