From 474629568c8b96b3883589a5dec8bba3cb0d53e0 Mon Sep 17 00:00:00 2001 From: Alexander Neundorf Date: Wed, 2 Jan 2008 16:52:12 -0500 Subject: [PATCH] ENH: check the magic code of the executable file to determine the executable file format. Tested for ELF on x86 Linux, COFF and Mach-O prepared but commented out since I don't have such systems available. Please have a look a CMakeDetermineCompilerId.cmake and enable the test for them too. Only add the option for using chrpath if the executable format is ELF Alex --- Modules/CMakeDetermineCompilerId.cmake | 32 ++++++++++++++++++++++++++ Modules/CMakeFindBinUtils.cmake | 8 +++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake index b3417d328..f8dab51ad 100644 --- a/Modules/CMakeDetermineCompilerId.cmake +++ b/Modules/CMakeDetermineCompilerId.cmake @@ -77,6 +77,31 @@ MACRO(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) "Compilation of the ${lang} compiler identification source \"" "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \"" "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n") + + # try to figure out the executable format: ELF, COFF, Mach-O + IF(NOT CMAKE_EXECUTABLE_FORMAT) + FILE(READ ${CMAKE_${lang}_COMPILER_ID_EXE} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX) + + # ELF files start with 0x7f"ELF" + IF("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46") + SET(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE STRING "Executable file format") + ENDIF("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46") + +# # COFF (.exe) files start with "MZ" +# IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....") +# SET(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format") +# ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....") +# +# # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html +# IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe") +# SET(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format") +# ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe") +# IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface") +# SET(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format") +# ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface") + + ENDIF(NOT CMAKE_EXECUTABLE_FORMAT) + # only check if we don't have it yet IF(NOT CMAKE_${lang}_COMPILER_ID) # Read the compiler identification string from the executable file. @@ -108,6 +133,13 @@ MACRO(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) ENDIF(NOT CMAKE_${lang}_COMPILER_ID) ENDFOREACH(CMAKE_${lang}_COMPILER_ID_EXE) + # if the format is unknown after all files have been checked, put "Unknown" in the cache + IF(NOT CMAKE_EXECUTABLE_FORMAT) + SET(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE STRING "Executable file format") + ELSE(NOT CMAKE_EXECUTABLE_FORMAT) + MESSAGE(STATUS "The executable file format is ${CMAKE_EXECUTABLE_FORMAT}") + ENDIF(NOT CMAKE_EXECUTABLE_FORMAT) + IF(NOT COMPILER_${lang}_PRODUCED_FILES) # No executable was found. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log diff --git a/Modules/CMakeFindBinUtils.cmake b/Modules/CMakeFindBinUtils.cmake index af48ffd04..688d5d81f 100644 --- a/Modules/CMakeFindBinUtils.cmake +++ b/Modules/CMakeFindBinUtils.cmake @@ -75,13 +75,13 @@ IF(APPLE) MARK_AS_ADVANCED(CMAKE_INSTALL_NAME_TOOL) ENDIF(APPLE) -IF(UNIX AND NOT APPLE AND NOT WIN32) +# if we are on an ELF system, search for chrpath +IF("${CMAKE_EXECUTABLE_FORMAT}" STREQUAL "ELF") # on ELF platforms there might be chrpath, which works similar to install_name_tool - SET(CMAKE_USE_CHRPATH OFF CACHE BOOL "Enable this to use chrpath if available") + OPTION(CMAKE_USE_CHRPATH "Enable this to use chrpath if available" OFF) FIND_PROGRAM(CMAKE_CHRPATH chrpath PATHS ${_CMAKE_TOOLCHAIN_LOCATION} NO_DEFAULT_PATH) FIND_PROGRAM(CMAKE_CHRPATH chrpath) MARK_AS_ADVANCED(CMAKE_CHRPATH) -ENDIF(UNIX AND NOT APPLE AND NOT WIN32) - +ENDIF("${CMAKE_EXECUTABLE_FORMAT}" STREQUAL "ELF")