0

I want to pass result of the getconf PAGESIZE command output as preprocessor define to my program in form of -DPAGESIZE=`getconf PAGESIZE` for [[gnu::assume_aligned(PAGESIZE)]] in custom allocator declaration.

I tried the following:

add_definitions(-DPAGESIZE=`getconf PAGESIZE`)

But it expanded exactly as -DPAGESIZE="\`getconf" ... PAGESIZE`, where ... is contents of other CMAKE_CXX_FLAGS*. I.e. there is an issue with escaping of backticks in CMakeLists.txt files.

How to properly pass such an arguments to compiler/linker in CMakeLists.txt files? Maybe is there another way to achieve desired?

Also I tried add_definitions(-DPAGESIZE="$$(getconf PAGESIZE)") ($$ expanded as $ by cmake), but -DPAGESIZE and the rest part are splitted by cmake. add_definitions("-DPAGESIZE=$$(getconf PAGESIZE)") makes cmake to escape every dollar sign though.

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • 1
    The answer here might be of use: http://stackoverflow.com/a/1468695/3171657 – Turn Dec 30 '15 at 06:41
  • @usr1234567 Backtick make sense in Bourne shell context. It make shell to run command enclosed in backticks, then to substitute it instead of whole "backticked" expression. – Tomilov Anatoliy Dec 30 '15 at 06:51
  • @Orient: I know but I doubt this will work with CMake. You can see that the backtick is escaped. So you have to verify that it works. – usr1234567 Dec 30 '15 at 06:58
  • Finally solved by using ``set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPAGESIZE=\"`getconf PAGESIZE`\"")``. Seems that escaping in contexts of `add_definitions()` and `set()` differs. – Tomilov Anatoliy Dec 30 '15 at 07:23

1 Answers1

1

According to documentation for add_definitions command, preprocessor definitions, passed to this command, are appended to COMPILE_DEFINITIONS property:

Flags beginning in -D or /D that look like preprocessor definitions are automatically added to the COMPILE_DEFINITIONS directory property for the current directory.

And content of COMPILE_DEFINITIONS property, according to its documentation is always escaped by CMake, so you cannot preserve special meaning of backticks in the build command:

CMake will automatically escape the value correctly for the native build system

Your may modify CMAKE_CXX_FLAGS manually, as you show in your comment.

The better way is to use execute_process command for run needed command at configuration stage, and use its output for add_definitions command. (Or use this output for create additional header file with configure_file).

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153