1

I need to do following.

Source code is located in

src\MY_SOURCE_PATH\SourceFiles

I need to expand macros, especially because I use macros to generate some source code (guards for serializing enums in example, also the same thing is done by some libraries I use (Offirmo, SparkParticleEngine) but I don't want macros polluting my users namespace nor write a lot of code by hand.

So I need to preprocess some macros (not all, for example I don't want to expand things like "assert" or include guards) and create a clone of my source dir.

src_expanded\MY_SOURCE_PATH\SourceFilesWithLessMacros

then I would run "build" on that new directory, and finally I want to move headers that match a particular REGEX (headers prefixed with "libraryPrefix") on a Include Directory

include\MY_SOURCE_PATH\HeadersMarkedAsPublicIncludes

so that users cannot include implementation headers

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • To not expand certain macros, probably I'd better workaround (for example undef assert and replace with a dummy function?) – CoffeDeveloper Nov 28 '14 at 10:17
  • 1
    SO works best in "one question per question" mode, and the question about header files is really independent of the macro one. I suggest you remove it from this question, have a look at such CMake commands as [`file(COPY)`](http://www.cmake.org/cmake/help/v3.1/command/file.html) or [`add_custom_command(TARGET tgt POST_BUILD ...)`](http://www.cmake.org/cmake/help/v3.1/command/add_custom_command.html), and if you still need help, ask a separate question here. – Angew is no longer proud of SO Nov 28 '14 at 10:27
  • ok thanks, I did it, now the macro part is stripped out. – CoffeDeveloper Nov 28 '14 at 11:54
  • Now your question does not match my answer at all; sorry, I will roll your edit back. Stack Overflow is about building a lasting Q&A database, not just about answering the questions here and now. You should take the question's current text (it will still be accessible in edit history) and ask it as a new question. – Angew is no longer proud of SO Nov 28 '14 at 12:00

1 Answers1

2

Expanding only some macros sounds rather complicated. If you'd be content with just "I want to generate some parts of the file" in a different way than preprocessor macros, you could use CMake's command configure_file.

What the command does is parse a file and substitute CMake variables inside the file. Here's an example of what your configured.cpp file might look like:

#include <cassert>

void ${SET_BY_CMAKE}(int x) {
  assert(x == 42);
}

In CMakeLists.txt, you'd do the following:

set(SET_BY_CMAKE foo)
configure_file(configured.cpp configured.cpp)
add_executable(MyExe ${CMAKE_CURRENT_BINARY_DIR}/configured.cpp other.cpp files.cpp)

The call to configure_file will create a configured.cpp file in your binary directory which will look like this:

#include <cassert>

void foo(int x) {
  assert(x == 42);
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455