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
This commit is contained in:
Alexander Neundorf 2008-01-02 16:52:12 -05:00
parent 7b54af713d
commit 474629568c
2 changed files with 36 additions and 4 deletions

View File

@ -77,6 +77,31 @@ MACRO(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
"Compilation of the ${lang} compiler identification source \"" "Compilation of the ${lang} compiler identification source \""
"${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \"" "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \""
"${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n") "${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 # only check if we don't have it yet
IF(NOT CMAKE_${lang}_COMPILER_ID) IF(NOT CMAKE_${lang}_COMPILER_ID)
# Read the compiler identification string from the executable file. # 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) ENDIF(NOT CMAKE_${lang}_COMPILER_ID)
ENDFOREACH(CMAKE_${lang}_COMPILER_ID_EXE) 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) IF(NOT COMPILER_${lang}_PRODUCED_FILES)
# No executable was found. # No executable was found.
FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log

View File

@ -75,13 +75,13 @@ IF(APPLE)
MARK_AS_ADVANCED(CMAKE_INSTALL_NAME_TOOL) MARK_AS_ADVANCED(CMAKE_INSTALL_NAME_TOOL)
ENDIF(APPLE) 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 # 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 PATHS ${_CMAKE_TOOLCHAIN_LOCATION} NO_DEFAULT_PATH)
FIND_PROGRAM(CMAKE_CHRPATH chrpath) FIND_PROGRAM(CMAKE_CHRPATH chrpath)
MARK_AS_ADVANCED(CMAKE_CHRPATH) MARK_AS_ADVANCED(CMAKE_CHRPATH)
ENDIF(UNIX AND NOT APPLE AND NOT WIN32) ENDIF("${CMAKE_EXECUTABLE_FORMAT}" STREQUAL "ELF")