[CMake] Setting environment variables prebuild

kent williams nkwmailinglists at gmail.com
Tue Mar 3 10:32:47 EST 2009


You're not missing anything per se.  If you set environment variables
in your CMakeLists.txt file for some operations it's just too late. In
particular, it doesn't add commands to the build files it generates to
set the environment variables the way you need them.

You can get around this -- I think -- by setting the QNX environment
variables before you run CMake, and making sure they're set before you
try and build or use Eclipse.  CMake also will honor CXX, CC, CXXFLAGS
and CFLAGS environment variables, using them to initialize the
corresponding CMAKE variables.

This is the sort of squirrelly messing about that is an inevitable
part of being a software developer. That's why they pay you the big
bucks.

On Tue, Mar 3, 2009 at 9:13 AM, Kito Berg-Taylor
<kito.berg-taylor at dlr.de> wrote:
> Hello all,
>
> I'm very new to cmake, so excuse me if I'm missing some obvious
> functionality that cmake already has.
>
> I am building a project using a QNX cross-compile toolchain that I
> setup. For those not familiar with the QNX toolchain, the QNX compiler
> qcc is a wrapper around gcc that's mostly the same but has a few
> peculiarities. Of particular relevance, it requires QNX_HOST and
> QNX_TARGET (and QNX_CONFIGURATION on win32) to be set as environment
> variables. I have setup my toolchain_qnx640.cmake file to automatically
> locate the $QNX_HOST (...etc) path and then set it as follows:
>
> SET( ENV{QNX_HOST} ${PATH_TO_QNX_HOST} )
>
> This allows the configure/generate steps to work correctly, however when
> I go to build the project unless these variables are set separately in
> the environment, the build will fail. In other words, CMake is not
> setting this variable as part of the build (although it did set them
> when it went to test the compiler, because qcc passed the compiler
> test).
>
> When building from UNIX Makefiles I can set the variable at the console
> and then "make" and it will work. When using Codeblocks projects if (and
> only if) I change the project compiler to QCC Compiler it will also
> build, since Codeblocks knows how to setup the environment for QCC. I
> can't find a way to make QCC the default compiler when generating the
> project (otherwise this will get overridden every time the project is
> re-generated). Eclipse CDT projects similarly won't build, unless I mess
> around with the project settings (new to eclipse, haven't found the
> right settings yet).
>
> Am I missing something? Is this a bug/design choice?
>
> I realize that the simple answer is to just set these environment
> variables on the machines before building, but this is not always
> possible (and shouldn't be necessary).
>
> I've included the toolchain_qnx640.cmake file below in case it makes
> things a little clearer. I welcome any input...
>
> SET( CMAKE_SYSTEM_NAME Qnx )
> SET( CMAKE_SYSTEM_VERSION 6.4.0 )
> SET( CMAKE_SYSTEM_PROCESSOR x86 )
> SET( TOOLCHAIN QNX )
>
> # used to help us find the programs
> # CMAKE_EXECUTABLE_SUFFIX wasn't working
> # because it found the target suffix, which
> # is not what we want here
> IF( CMAKE_HOST_WIN32 )
>  SET( HOST_EXECUTABLE_SUFFIX ".exe" )
> ENDIF( CMAKE_HOST_WIN32 )
>
> # see if we can find QNX_HOST and QNX_TARGET
> # this is not the most intelligent search, but it should
> # serve our purposes for now
> FIND_PATH( QNX_HOST
>  NAME usr/bin/qcc${HOST_EXECUTABLE_SUFFIX}
>  PATHS $ENV{QNX_HOST} C:/Programme/CodeBlocks/QNXSDK630/host/win32/x86
>  NO_CMAKE_PATH
>  NO_CMAKE_ENVIRONMENT_PATH
> )
>
> FIND_PATH( QNX_TARGET
>  NAME usr/include/qnx_errno.h
>  PATHS $ENV{QNX_TARGET} C:/Programme/CodeBlocks/QNXSDK630/target/qnx6
>  NO_CMAKE_PATH
>  NO_CMAKE_ENVIRONMENT_PATH
> )
>
> # QNX_CONFIGURATION only exists in windows
> IF( CMAKE_HOST_WIN32 )
>  FIND_PATH( QNX_CONFIGURATION
>    NAME bin/qnxactivate.exe
>    PATHS $ENV{QNX_CONFIGURATION}
> C:/Programme/CodeBlocks/QNXSDK630/config
>    NO_CMAKE_PATH
>    NO_CMAKE_ENVIRONMENT_PATH
>  )
> ENDIF( CMAKE_HOST_WIN32 )
>
> # export the QNX flags so that QNX can use them too
> # this is necessary for qcc to work, otherwise it will
> # complain about the license and missing QNX_HOST,QNX_TARGET
> SET( ENV{QNX_HOST} ${QNX_HOST} )
> SET( ENV{QNX_TARGET} ${QNX_TARGET} )
> IF( CMAKE_HOST_WIN32 )
>  SET( ENV{QNX_CONFIGURATION} ${QNX_CONFIGURATION} )
>
>  # cygwin1.dll needs to be in the PATH for qcc to run
>  SET( ENV{PATH} "$ENV{PATH};${QNX_HOST}/usr/bin" )
> ENDIF( CMAKE_HOST_WIN32 )
>
> # Find some basic programs
> SET( CMAKE_MAKE_PROGRAM "${QNX_HOST}/usr/bin/make
> ${HOST_EXECUTABLE_SUFFIX}"    CACHE PATH "QNX Make Program" )
> SET( CMAKE_SH           "${QNX_HOST}/usr/bin/sh
> ${HOST_EXECUTABLE_SUFFIX}"      CACHE PATH "QNX shell Program" )
> SET( CMAKE_AR           "${QNX_HOST}/usr/bin/ntox86-ar
> ${HOST_EXECUTABLE_SUFFIX}"      CACHE PATH "QNX ar Program" )
> SET( CMAKE_NM           "${QNX_HOST}/usr/bin/ntox86-nm
> ${HOST_EXECUTABLE_SUFFIX}"      CACHE PATH "QNX nm Program" )
> SET( CMAKE_OBJCOPY      "${QNX_HOST}/usr/bin/ntox86-objcopy
> ${HOST_EXECUTABLE_SUFFIX}" CACHE PATH "QNX objcopy Program" )
> SET( CMAKE_OBJDUMP      "${QNX_HOST}/usr/bin/ntox86-objdump
> ${HOST_EXECUTABLE_SUFFIX}" CACHE PATH "QNX objdump Program" )
> SET( CMAKE_LINKER       "${QNX_HOST}/usr/bin/qcc
> ${HOST_EXECUTABLE_SUFFIX}"     CACHE PATH "QNX Linker Program" )
> SET( CMAKE_STRIP        "${QNX_HOST}/usr/bin/ntox86-strip
> ${HOST_EXECUTABLE_SUFFIX}"   CACHE PATH "QNX Strip Program" )
>
> SET( CMAKE_C_COMPILER ${QNX_HOST}/usr/bin/qcc${HOST_EXECUTABLE_SUFFIX} )
> SET( CMAKE_CXX_COMPILER ${QNX_HOST}/usr/bin/qcc
> ${HOST_EXECUTABLE_SUFFIX} )
>
> SET( CMAKE_C_FLAGS "-Vgcc_ntox86 -w1" CACHE STRING "qcc c flags" FORCE )
> SET( CMAKE_CXX_FLAGS "-Vgcc_ntox86 -lang-c++ -w1" CACHE STRING "qcc cxx
> flags" FORCE )
>
> SET( CMAKE_FIND_ROOT_PATH ${QNX_TARGET} )
> SET( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
> SET( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
> SET( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>


More information about the CMake mailing list