Commit 59f879ad authored by Hans Wennborg's avatar Hans Wennborg
Browse files

Merging r242298:

------------------------------------------------------------------------
r242298 | jlpeyton | 2015-07-15 09:05:30 -0700 (Wed, 15 Jul 2015) | 39 lines

Large Refactor of CMake build system

This commit improves numerous functionalities of the OpenMP CMake build 
system to be more conducive with LLVM's build system and build philosophies.
The CMake build system, as it was before this commit, was not up to LLVM's 
standards and did not implement the configuration stage like most CMake based
build systems offer (check for compiler flags, libraries, etc.) In order to
improve it dramatically in a short period of time, a large refactoring had 
to be done.
The main changes done with this commit are as follows:

* Compiler flag checks - The flags are no longer grabbed from compiler specific
  directories.  They are checked for availability in config-ix.cmake and added
  accordingly inside LibompHandleFlags.cmake.
* Feature checks were added in config-ix.cmake.  For example, the standard CMake
  module FindThreads is probed for the threading model to use inside the OpenMP
  library.
* OS detection - There is no longer a LIBOMP_OS variable, OS-specifc build logic
  is wrapped around the WIN32 and APPLE macros with !(WIN32 OR APPLE) meaning 
  a Unix flavor of some sort.
* Got rid of vestigial functions/macros/variables
* Added new libomp_append() function which is used everywhere to conditionally
  or undconditionally append to a list
* All targets have the libomp prefix so as not to interfere with any other
  project
* LibompCheckLinkerFlag.cmake module was added which checks for linker flags
  specifically for building shared libraries.
* LibompCheckFortranFlag.cmake module was added which checks for fortran flag
  availability.
* Removed most of the cruft from the translation between the perl+Makefile based
  build system and this one.  The remaining components that they share are
  perl scripts which I'm in the process of removing.

There is still more left to do.  The perl scripts still need to be removed, and
a config.h.in file (or similarly named) needs to be added with #cmakedefine lines
in it.  But this is a much better first step than the previous system.

Differential Revision: http://reviews.llvm.org/D10656

------------------------------------------------------------------------

llvm-svn: 242335
parent ebfe2a27
Loading
Loading
Loading
Loading
+166 −468

File changed.

Preview size limit exceeded, changes collapsed.

+0 −112
Original line number Diff line number Diff line
#
#//===----------------------------------------------------------------------===//
#//
#//                     The LLVM Compiler Infrastructure
#//
#// This file is dual licensed under the MIT and the University of Illinois Open
#// Source Licenses. See LICENSE.txt for details.
#//
#//===----------------------------------------------------------------------===//
#

###############################################################################
# This file contains additional build rules that correspond to build.pl's rules.
# Building libomp.dbg is linux only, Windows will build libompmd.dll.pdb
# This file is only active if ${LIBOMP_USE_BUILDPL_RULES} is true.
#
#                        ######### BUILD DEPENDENCIES ##########
#
#        exports/.../libomp.so                        exports/.../libomp.dbg
#        [copy]  |                                                 | [copy]
#                |                                                 |
#           ./libomp.so                                     ./libomp.dbg
#    [copy]    /  OR  \____________ [copy]                         | [copy]
#             /                    \                               |
#    ./unstripped/libomp.so   ./stripped/libomp.so   ./unstripped/libomp.dbg
#           /                                \                /
#          / [linking]                        \[strip]       /[strip and store]
#         /                                    \            /
#     ${objs} (maybe compiled with -g)     ./unstripped/libomp.so (library with debug info in it)
#                                                    |
#                                                    | [linking]
#                                                    |
#                                                 ${objs} (always compiled with -g)
#
# For icc Linux builds, we always include debugging information via -g and create libomp.dbg 
# so that Intel(R) Parallel Amplifier can use the .dbg file.
# For icc Windows builds, we always include debugging information via -Zi and create libomp.pdb
# in a fashion similar to libomp.dbg
# For icc Mac builds, we don't bother with the debug info.

# We build library in unstripped directory
file(MAKE_DIRECTORY ${build_dir}/unstripped)

# Only build the .dbg file for Release builds
# Debug and RelWithDebInfo builds should not create a .dbg file.  
# The debug info should remain in the library file.
if(${LINUX} AND ${RELEASE_BUILD})
    set(dbg_file ${lib_item}.dbg)
endif()

################################
# --- Create $(lib_file).dbg ---
if(NOT "${dbg_file}" STREQUAL "")
    # if a ${dbg_file} file is going to be created, then 
    file(MAKE_DIRECTORY ${build_dir}/stripped)

    # ./${lib_file} : stripped/${lib_file}
    #     copy stripped/${lib_file} ./${lib_file}
    simple_copy_recipe("${lib_file}"   "${build_dir}/stripped"   "${build_dir}")

    # stripped/${lib_file} : unstripped/${lib_file} ./${dbg_file}
    #     objcopy --strip-debug unstripped/${lib_file} stripped/${lib_file}.tmp
    #     objcopy --add-gnu-debuglink=${dbg_file} stripped/${lib_file}.tmp stripped/${lib_file}
    add_custom_command(
        OUTPUT  ${build_dir}/stripped/${lib_file}
        COMMAND ${CMAKE_OBJCOPY} --strip-debug ${build_dir}/unstripped/${lib_file} ${build_dir}/stripped/${lib_file}.tmp
        COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=${dbg_file} ${build_dir}/stripped/${lib_file}.tmp ${build_dir}/stripped/${lib_file}
        DEPENDS "${build_dir}/${dbg_file}"
    )

    # ./${dbg_file} : unstripped/${dbg_file}
    #     copy unstripped/${dbg_file} ./${dbg_file}
    simple_copy_recipe("${dbg_file}"   "${build_dir}/unstripped" "${build_dir}")

    # unstripped/${dbg_file} : unstripped/${lib_file}
    #     objcopy --only-keep-debug unstripped/${lib_file} unstripped/${dbg_file}
    add_custom_command(
        OUTPUT  ${build_dir}/unstripped/${dbg_file}
        COMMAND ${CMAKE_OBJCOPY} --only-keep-debug ${build_dir}/unstripped/${lib_file} ${build_dir}/unstripped/${dbg_file} 
        DEPENDS omp
    )
    
else()

    # ./${lib_file} : unstripped/${lib_file}
    #      copy unstripped/${lib_file} ./${lib_file}
    simple_copy_recipe("${lib_file}"   "${build_dir}/unstripped"  "${build_dir}")
endif()

# Windows specific command to move around debug info files post-build
if(NOT "${pdb_file}" STREQUAL "" AND ${RELEASE_BUILD})
    add_custom_command(TARGET omp POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E rename ${pdb_file} ${pdb_file}.nonstripped
        COMMAND ${CMAKE_COMMAND} -E rename ${pdb_file}.stripped ${pdb_file}
    )
endif()

# Have icc build libomp in unstripped directory
set_target_properties(omp PROPERTIES 
    LIBRARY_OUTPUT_DIRECTORY "${build_dir}/unstripped" 
    RUNTIME_OUTPUT_DIRECTORY "${build_dir}/unstripped"
    ARCHIVE_OUTPUT_DIRECTORY "${build_dir}"
)

# Always use RelWithDebInfo flags for Release builds when using the build.pl's build rules (use -g -O2 instead of just -O3)
# The debug info is then stripped out at the end of the build and put into libomp.dbg for Linux
if(${RELEASE_BUILD} AND NOT ${MAC})
    set(CMAKE_C_FLAGS_RELEASE   ${CMAKE_C_FLAGS_RELWITHDEBINFO}  )
    set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
    set(CMAKE_ASM_FLAGS_RELEASE ${CMAKE_ASM_FLAGS_RELWITHDEBINFO})
endif()
+0 −27
Original line number Diff line number Diff line
#
#//===----------------------------------------------------------------------===//
#//
#//                     The LLVM Compiler Infrastructure
#//
#// This file is dual licensed under the MIT and the University of Illinois Open
#// Source Licenses. See LICENSE.txt for details.
#//
#//===----------------------------------------------------------------------===//
#

# This file holds Clang (clang/clang++) specific compiler dependent flags
# The flag types are:
#   1) Assembly flags

#########################################################
# Assembly flags
function(append_assembler_specific_asm_flags input_asm_flags)
    set(local_asm_flags)
    append_asm_flags("-x assembler-with-cpp") # Assembly file that needs to be preprocessed
    if(${IA32})
        append_asm_flags("-m32") # Generate 32 bit IA-32 architecture code 
        append_asm_flags("-msse2") # Allow use of Streaming SIMD Instructions
    endif()
    set(${input_asm_flags} ${${input_asm_flags}} "${local_asm_flags}" PARENT_SCOPE)
endfunction()
+0 −50
Original line number Diff line number Diff line
#
#//===----------------------------------------------------------------------===//
#//
#//                     The LLVM Compiler Infrastructure
#//
#// This file is dual licensed under the MIT and the University of Illinois Open
#// Source Licenses. See LICENSE.txt for details.
#//
#//===----------------------------------------------------------------------===//
#

# This file holds Clang (clang/clang++) specific compiler dependent flags
# The flag types are:
#   1) C/C++ Compiler flags
#   2) Linker flags

#########################################################
# C/C++ Compiler flags
function(append_compiler_specific_c_and_cxx_flags input_c_flags input_cxx_flags)
    set(local_c_flags)
    set(local_cxx_flags)
    append_c_and_cxx_flags("-std=c++0x") # Enables support for many C++11 (formerly known as C++0x) features. The following are the most recently added features:
    append_c_and_cxx_flags("-fno-exceptions") # Exception handling table generation is disabled.
    append_c_and_cxx_flags("-x c++") # Compile C files as C++ files
    if(${IA32})
        append_c_and_cxx_flags("-m32") # Generate 32 bit IA-32 architecture code
        append_c_and_cxx_flags("-msse2") # Allow use of Streaming SIMD Instructions
    elseif(${ARM})
        append_c_and_cxx_flags("-marm") # Target the ARM architecture
    endif()
    append_c_and_cxx_flags("-Wno-unused-value") # Don't warn about unused values
    append_c_and_cxx_flags("-Wno-switch") # Don't warn about switch statements that don't cover entire range of values
    append_c_and_cxx_flags("-Wno-deprecated-register") # Don't warn about using register keyword
    set(${input_c_flags}   ${${input_c_flags}}   "${local_c_flags}" PARENT_SCOPE)
    set(${input_cxx_flags} ${${input_cxx_flags}} "${local_cxx_flags}" PARENT_SCOPE)
endfunction()

#########################################################
# Linker flags
function(append_compiler_specific_linker_flags input_ld_flags input_ld_flags_libs)
    set(local_ld_flags)
    set(local_ld_flags_libs)
    if(${IA32})
        append_linker_flags("-m32")
        append_linker_flags("-msse2")
    endif()
    set(${input_ld_flags}      ${${input_ld_flags}}      "${local_ld_flags}"       PARENT_SCOPE)
    set(${input_ld_flags_libs} ${${input_ld_flags_libs}} "${local_ld_flags_libs}"  PARENT_SCOPE)
endfunction()
+0 −138
Original line number Diff line number Diff line
#
#//===----------------------------------------------------------------------===//
#//
#//                     The LLVM Compiler Infrastructure
#//
#// This file is dual licensed under the MIT and the University of Illinois Open
#// Source Licenses. See LICENSE.txt for details.
#//
#//===----------------------------------------------------------------------===//
#

# This file holds the common flags independent of compiler
# The flag types are: 
#   1) Assembly flags          (append_asm_flags_common)
#   2) C/C++ Compiler flags    (append_c_and_cxx_flags_common)
#   3) Fortran Compiler flags  (append_fort_flags_common)
#   4) Linker flags            (append_linker_flags_common)
#   5) Archiver flags          (append_archiver_flags_common)

# These append_* macros all append to the corresponding list variable holding the flags.
macro(append_c_flags new_flag)
    list(APPEND local_c_flags    "${new_flag}")
endmacro()

macro(append_cxx_flags new_flag)
    list(APPEND local_cxx_flags  "${new_flag}")
endmacro()

macro(append_c_and_cxx_flags new_flag)
    append_c_flags("${new_flag}")
    append_cxx_flags("${new_flag}")
endmacro()

macro(append_asm_flags new_flag)
    list(APPEND local_asm_flags  "${new_flag}")
endmacro()

macro(append_fort_flags new_flag)
    list(APPEND local_fort_flags "${new_flag}")
endmacro()

# The difference between linker_flags and linker_flags_libs is linker_flags_libs
# is put at the end of the linker command where linking libraries should be done.
macro(append_linker_flags new_flag)
    list(APPEND local_ld_flags   "${new_flag}")
endmacro()

macro(append_linker_flags_library new_flag)
    list(APPEND local_ld_flags_libs "${new_flag}")
endmacro()

macro(append_archiver_flags new_flag)
    list(APPEND local_ar_flags   "${new_flag}")
endmacro()

#########################################################
# Global Assembly flags
function(append_asm_flags_common input_asm_flags)
    set(local_asm_flags)
    set(${input_asm_flags} "${${input_asm_flags}}" "${local_asm_flags}" "${LIBOMP_ASMFLAGS}" PARENT_SCOPE)
endfunction()

#########################################################
# Global C/C++ Compiler flags
function(append_c_and_cxx_flags_common input_c_flags input_cxx_flags)
    set(local_c_flags)
    set(local_cxx_flags)
    set(${input_c_flags}   "${${input_c_flags}}"   "${local_c_flags}"   "${LIBOMP_CFLAGS}"   PARENT_SCOPE)
    set(${input_cxx_flags} "${${input_cxx_flags}}" "${local_cxx_flags}" "${LIBOMP_CXXFLAGS}" PARENT_SCOPE)
endfunction()

#########################################################
# Global Fortran Compiler flags (for creating .mod files)
function(append_fort_flags_common input_fort_flags)
    set(local_fort_flags)
    set(${input_fort_flags} "${${input_fort_flags}}" "${local_fort_flags}" "${LIBOMP_FFLAGS}" PARENT_SCOPE)
endfunction()

#########################################################
# Linker flags
function(append_linker_flags_common input_ld_flags input_ld_flags_libs)
    set(local_ld_flags)
    set(local_ld_flags_libs)

    if(${LIBOMP_USE_PREDEFINED_LINKER_FLAGS})

        #################################
        # Windows linker flags
        if(${WINDOWS}) 

        ##################
        # MAC linker flags
        elseif(${MAC})
            append_linker_flags("-single_module")
            append_linker_flags("-current_version ${LIBOMP_VERSION}.0")
            append_linker_flags("-compatibility_version ${LIBOMP_VERSION}.0")
        #####################################################################################
        # Intel(R) Many Integrated Core Architecture (Intel(R) MIC Architecture) linker flags
        elseif(${MIC})
            append_linker_flags("-Wl,-x")
            append_linker_flags("-Wl,--warn-shared-textrel") #  Warn if the linker adds a DT_TEXTREL to a shared object.
            append_linker_flags("-Wl,--as-needed")
            append_linker_flags("-Wl,--version-script=${src_dir}/exports_so.txt") # Use exports_so.txt as version script to create versioned symbols for ELF libraries
            if(NOT ${STUBS_LIBRARY})
                append_linker_flags_library("-pthread") # link in pthread library
            endif()
            if(${LIBOMP_STATS})
                append_linker_flags_library("-Wl,-lstdc++") # link in standard c++ library (stats-gathering needs it)
            endif()
        #########################
        # Unix based linker flags
        else()
            # For now, always include --version-script flag on Unix systems.
            append_linker_flags("-Wl,--version-script=${src_dir}/exports_so.txt") # Use exports_so.txt as version script to create versioned symbols for ELF libraries
            append_linker_flags("-Wl,-z,noexecstack") #  Marks the object as not requiring executable stack.
            append_linker_flags("-Wl,--as-needed")    #  Only adds library dependencies as they are needed. (if libomp actually uses a function from the library, then add it)
            if(NOT ${STUBS_LIBRARY})
                append_linker_flags("-Wl,--warn-shared-textrel") #  Warn if the linker adds a DT_TEXTREL to a shared object.
                append_linker_flags("-Wl,-fini=__kmp_internal_end_fini") # When creating an ELF executable or shared object, call NAME when the 
                                                                         # executable or shared object is unloaded, by setting DT_FINI to the 
                                                                         # address of the function.  By default, the linker uses "_fini" as the function to call.
                append_linker_flags_library("-pthread") # link pthread library
            endif()
        endif() # if(${OPERATING_SYSTEM}) ...

    endif() # LIBOMP_USE_PREDEFINED_LINKER_FLAGS

    set(${input_ld_flags}      "${${input_ld_flags}}"      "${local_ld_flags}"      "${LIBOMP_LDFLAGS}"     PARENT_SCOPE)
    set(${input_ld_flags_libs} "${${input_ld_flags_libs}}" "${local_ld_flags_libs}" "${LIBOMP_LIBFLAGS}" PARENT_SCOPE)
endfunction()

#########################################################
# Archiver Flags
function(append_archiver_flags_common input_ar_flags)
    set(local_ar_flags)
    set(${input_ar_flags} "${${input_ar_flags}}" "${local_ar_flags}" PARENT_SCOPE)
endfunction()
Loading