Q:

How to compile source files with different-2 flags in cmake?

I am migrating my make build system to cmake so I need to compile the source files with different (2) flags and then create the static library. 
For example I have:
$(OBJSGEN): $(OBJDIR1)/%.o: $(SRCDIR)/%.cpp 

$(CC) $(FLAGS) $(CFLAGS) $(DFLAGS) $(EFLAGS1) $(INCLUDES) -c $<  -o $@

Can any one please help me out?

A:

You can use CMake's object libraries to do this:
# compiles source.cpp with -DSOME_DEF=1
add_library(obj1 OBJECT source.cpp)

# This is the "modern" syntax for setting preprocessor definitions:
target_compile_definitions(obj1 PRIVATE SOME_DEF=1)

# compiles source.cpp with -DSOME_DEF=2
add_library(obj2 OBJECT source.cpp)

# This is the "old" way of setting preprocessor definitions:
set_target_properties(obj2 PROPERTIES
    COMPILE_DEFINITIONS SOME_DEF=2)

# creates a static library with both objects
add_library(lib STATIC
    $<TARGET_OBJECTS:obj1>
    $<TARGET_OBJECTS:obj2>)

Edit: I included CMake 2.8 compatible way of setting preprocessor definitions per target using set_target_properties.

