[CMake] Cmake 2.8 and cross compilation

Alexander Neundorf a.neundorf-work at gmx.net
Mon Nov 16 17:22:04 EST 2009


On Monday 16 November 2009, Emmanuel Blot wrote:
> > If the file needs to be preprocessed, set the LANGUAGE source file
> > property to C, this should work in most cases for now.
>
> Yes they (many of them) do
>
> Is there anyway in CMake to tell something like "all .S file should be
> built with GCC" or something like this, so that developers do not have
> to cope with CMake bolts and nuts for every asm file they add to a
> project?
>
> I'm not sure to understand how to build .S files with some compiler
> option switches, and C files with other option switches.
> As an example C and C++ languages may use distinct options using
> CMAKE_C_... anc CMAKE_CXX_ flags, but how this work if I tell CMake
> that a S file is a C file, which is not?

please have a look at the attached file and replace the 
CMakeDetermineASMCompiler.cmake which comes with cmake with it.
Then enable the language "ASM" (not ASM-ATT) and then all *.s, *.S and *.asm 
files should be processed by the C compiler with separate ASM-specific flags.
Let me know if you think this approach makes sense (i.e. use the C compiler 
for asm files, and ASM-ATT if they are really plain assembler files with no 
preprocessing etc.)

Alex
-------------- next part --------------

#=============================================================================
# Copyright 2007-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distributed this file outside of CMake, substitute the full
#  License text for the above reference.)

# determine the compiler to use for ASM programs

IF(NOT CMAKE_ASM${ASM_DIALECT}_COMPILER)
  # prefer the environment variable ASM${ASM_DIALECT}
  IF($ENV{ASM${ASM_DIALECT}} MATCHES ".+")
    SET(CMAKE_ASM${ASM_DIALECT}_COMPILER_INIT "$ENV{ASM${ASM_DIALECT}}")
  ENDIF($ENV{ASM${ASM_DIALECT}} MATCHES ".+")

  # finally list compilers to try
  IF(CMAKE_ASM${ASM_DIALECT}_COMPILER_INIT)
    SET(CMAKE_ASM${ASM_DIALECT}_COMPILER_LIST ${CMAKE_ASM${ASM_DIALECT}_COMPILER_INIT})
    # Find the compiler.
    FIND_PROGRAM(CMAKE_ASM${ASM_DIALECT}_COMPILER NAMES ${CMAKE_ASM${ASM_DIALECT}_COMPILER_LIST} HINTS ${_CMAKE_USER_C_COMPILER_PATH} ${_CMAKE_USER_CXX_COMPILER_PATH} ${_CMAKE_TOOLCHAIN_LOCATION} DOC "The assembler to use")

  ELSE(CMAKE_ASM${ASM_DIALECT}_COMPILER_INIT)
    # If there is no assembler name preset, try to use the C/C++ compiler also for the assembler files
    IF(CMAKE_C_COMPILER)
      SET(CMAKE_ASM${ASM_DIALECT}_COMPILER ${CMAKE_C_COMPILER} CACHE FILEPATH "The assembler to use")
      SET(CMAKE_ASM${ASM_DIALECT}_COMPILER_ID ${CMAKE_C_COMPILER_ID})
    ELSE(CMAKE_C_COMPILER)
      IF(CMAKE_CXX_COMPILER)
        SET(CMAKE_ASM${ASM_DIALECT}_COMPILER ${CMAKE_CXX_COMPILER} CACHE FILEPATH "The assembler to use")
        SET(CMAKE_ASM${ASM_DIALECT}_COMPILER_ID ${CMAKE_CXX_COMPILER_ID})
      ENDIF(CMAKE_CXX_COMPILER)
    ENDIF(CMAKE_C_COMPILER)
    SET(CMAKE_C_COMPILER)
  ENDIF(CMAKE_ASM${ASM_DIALECT}_COMPILER_INIT)

ELSE(NOT CMAKE_ASM${ASM_DIALECT}_COMPILER)

  # we only get here if CMAKE_ASM${ASM_DIALECT}_COMPILER was specified using -D or a pre-made CMakeCache.txt
  # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  #
  # if a compiler was specified by the user but without path, 
  # now try to find it with the full path
  # if it is found, force it into the cache, 
  # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  GET_FILENAME_COMPONENT(_CMAKE_USER_ASM${ASM_DIALECT}_COMPILER_PATH "${CMAKE_ASM${ASM_DIALECT}_COMPILER}" PATH)
  IF(NOT _CMAKE_USER_ASM${ASM_DIALECT}_COMPILER_PATH)
    FIND_PROGRAM(CMAKE_ASM${ASM_DIALECT}_COMPILER_WITH_PATH NAMES ${CMAKE_ASM${ASM_DIALECT}_COMPILER})
    MARK_AS_ADVANCED(CMAKE_ASM${ASM_DIALECT}_COMPILER_WITH_PATH)
    IF(CMAKE_ASM${ASM_DIALECT}_COMPILER_WITH_PATH)
      SET(CMAKE_ASM${ASM_DIALECT}_COMPILER ${CMAKE_ASM${ASM_DIALECT}_COMPILER_WITH_PATH} CACHE FILEPATH "Assembler" FORCE)
    ENDIF(CMAKE_ASM${ASM_DIALECT}_COMPILER_WITH_PATH)
  ENDIF(NOT _CMAKE_USER_ASM${ASM_DIALECT}_COMPILER_PATH)
ENDIF(NOT CMAKE_ASM${ASM_DIALECT}_COMPILER)
MARK_AS_ADVANCED(CMAKE_ASM${ASM_DIALECT}_COMPILER)

IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_ASM${ASM_DIALECT}_COMPILER}" PATH)
ENDIF (NOT _CMAKE_TOOLCHAIN_LOCATION)

# If we have a gas/as cross compiler, they have usually some prefix, like 
# e.g. powerpc-linux-gas, arm-elf-gas or i586-mingw32msvc-gas .
# The other tools of the toolchain usually have the same prefix
# NAME_WE cannot be used since then this test will fail for names lile
# "arm-unknown-nto-qnx6.3.0-gas.exe", where BASENAME would be 
# "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
IF (NOT _CMAKE_TOOLCHAIN_PREFIX)
  GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_ASM${ASM_DIALECT}_COMPILER}" NAME)
  IF (COMPILER_BASENAME MATCHES "^(.+-)g?as(\\.exe)?$")
    STRING(REGEX REPLACE "^(.+-)g?as(\\.exe)?$"  "\\1" _CMAKE_TOOLCHAIN_PREFIX "${COMPILER_BASENAME}")
  ENDIF (COMPILER_BASENAME MATCHES "^(.+-)g?as(\\.exe)?$")
ENDIF (NOT _CMAKE_TOOLCHAIN_PREFIX)

INCLUDE(CMakeFindBinUtils)

SET(CMAKE_ASM${ASM_DIALECT}_COMPILER_ENV_VAR "ASM${ASM_DIALECT}")

IF(CMAKE_ASM${ASM_DIALECT}_COMPILER)
  MESSAGE(STATUS "Found assembler: ${CMAKE_ASM${ASM_DIALECT}_COMPILER}")
ELSE(CMAKE_ASM${ASM_DIALECT}_COMPILER)
  MESSAGE(STATUS "Didn't find assembler")
ENDIF(CMAKE_ASM${ASM_DIALECT}_COMPILER)


SET(_CMAKE_ASM_COMPILER "${CMAKE_ASM${ASM_DIALECT}_COMPILER}")
SET(_CMAKE_ASM_COMPILER_ARG1 "${CMAKE_ASM${ASM_DIALECT}_COMPILER_ARG1}")
SET(_CMAKE_ASM_COMPILER_ENV_VAR "${CMAKE_ASM${ASM_DIALECT}_COMPILER_ENV_VAR}")

# configure variables set in this file for fast reload later on
CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeASMCompiler.cmake.in 
  ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeASM${ASM_DIALECT}Compiler.cmake IMMEDIATE @ONLY)

SET(_CMAKE_ASM_COMPILER)
SET(_CMAKE_ASM_COMPILER_ARG1)
SET(_CMAKE_ASM_COMPILER_ENV_VAR)


More information about the CMake mailing list