Adding an individual define to each source file of a library

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding an individual define to each source file of a library

George PF
Hello,

following "modern cmake" conventions I want to create a library where every single file is compiled with an individual define (-D_fileX_, required for a macro which integrates code into every translation unit).

So following this

    add_library(mylib SHARED file1.c file2.c)
    target_link_libraries(mylib PUBLIC otherlib)

I'd like to iterate over all "mylib" source file targets and modify them so that the compiler is called like this:

    cc -c -o file1.o file1.cc -D_file1_define_
    cc -c -o file1.o file2.cc -D_file2_define_
    etc.

Is this possible?


Regards

GPF
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Adding an individual define to each source file of a library

Eric Noulard


Le jeu. 23 août 2018 à 13:02, George PF <[hidden email]> a écrit :
Hello,

following "modern cmake" conventions I want to create a library where every single file is compiled with an individual define (-D_fileX_, required for a macro which integrates code into every translation unit).

So following this

    add_library(mylib SHARED file1.c file2.c)
    target_link_libraries(mylib PUBLIC otherlib)

I'd like to iterate over all "mylib" source file targets and modify them so that the compiler is called like this:

    cc -c -o file1.o file1.cc -D_file1_define_
    cc -c -o file1.o file2.cc -D_file2_define_
    etc.

Is this possible?

You can retrieve the sources associated with a target using SOURCES properties.

then 
with
or

for setting property:

on each source file.

not sure why you want to do that you know that compiler already defined many "standard" macro for you, like __FILE__

--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Adding an individual define to each source file of a library

George PF
Thank you for the precise pointers, I added to the same CMakeLists.txt:
 
    get_property(mysrcs TARGET mylib PROPERTY SOURCES)

    foreach(x IN LISTS mysrcs)
        message("at ${x}")
        set_property(SOURCE x APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")
    #    set_source_files_properties(x PROPERTIES COMPILE_DEFINITIONS "TEST1;TEST2;") # also not working
    endforeach(x)

However, whatever I write into 'set_property()' - no APPEND, single entry, not quoted - does not end up
on the compiler command line. Is there a type mismatch which is silently ignored?

As to __FILE__ or similar, the define is used as a variable, i.e. the .c suffix would have to be removed
via the c processor. The current directory is also used. I think replacing .c and appending the name of
the current directory will be easier in cmake.


> Le jeu. 23 août 2018 à 13:02, George PF <george.p.f at mail.com> a écrit :
>
> > Hello,
> >
> > following "modern cmake" conventions I want to create a library where
> > every single file is compiled with an individual define (-D_fileX_,
> > required for a macro which integrates code into every translation unit).
> >
> > So following this
> >
> >     add_library(mylib SHARED file1.c file2.c)
> >     target_link_libraries(mylib PUBLIC otherlib)
> >
> > I'd like to iterate over all "mylib" source file targets and modify them
> > so that the compiler is called like this:
> >
> >     cc -c -o file1.o file1.cc -D_file1_define_
> >     cc -c -o file1.o file2.cc -D_file2_define_
> >     etc.
> >
> > Is this possible?
> >
>
> You can retrieve the sources associated with a target using SOURCES
> properties.
> https://cmake.org/cmake/help/latest/prop_tgt/SOURCES.html
>
> then
> https://cmake.org/cmake/help/latest/command/foreach.html
> with
> https://cmake.org/cmake/help/latest/command/set_property.html
> or
> https://cmake.org/cmake/help/latest/command/set_source_files_properties.html
>
> for setting property:
> https://cmake.org/cmake/help/latest/prop_sf/COMPILE_DEFINITIONS.html
>
> on each source file.
>
> not sure why you want to do that you know that compiler already defined
> many "standard" macro for you, like __FILE__
> https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros
> ?
>
> --
> Eric
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Adding an individual define to each source file of a library

Eric Noulard


Le jeu. 23 août 2018 à 18:31, George PF <[hidden email]> a écrit :
Thank you for the precise pointers, I added to the same CMakeLists.txt:

    get_property(mysrcs TARGET mylib PROPERTY SOURCES)

    foreach(x IN LISTS mysrcs)
        message("at ${x}")
        set_property(SOURCE x APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")
    #    set_source_files_properties(x PROPERTIES COMPILE_DEFINITIONS "TEST1;TEST2;") # also not working
    endforeach(x)

However, whatever I write into 'set_property()' - no APPEND, single entry, not quoted - does not end up
on the compiler command line. Is there a type mismatch which is silently ignored?

Or you are doing this in a directory which is not the one where the target is defined and from the doc:
$ cmake --help-command set_property

...
``SOURCE``
  Scope may name zero or more source files.  Note that source
  file properties are visible only to targets added in the same
  directory (CMakeLists.txt).

...


As to __FILE__ or similar, the define is used as a variable, i.e. the .c suffix would have to be removed
via the c processor. The current directory is also used. I think replacing .c and appending the name of
the current directory will be easier in cmake.


> Le jeu. 23 août 2018 à 13:02, George PF <george.p.f at mail.com> a écrit :
>
> > Hello,
> >
> > following "modern cmake" conventions I want to create a library where
> > every single file is compiled with an individual define (-D_fileX_,
> > required for a macro which integrates code into every translation unit).
> >
> > So following this
> >
> >     add_library(mylib SHARED file1.c file2.c)
> >     target_link_libraries(mylib PUBLIC otherlib)
> >
> > I'd like to iterate over all "mylib" source file targets and modify them
> > so that the compiler is called like this:
> >
> >     cc -c -o file1.o file1.cc -D_file1_define_
> >     cc -c -o file1.o file2.cc -D_file2_define_
> >     etc.
> >
> > Is this possible?
> >
>
> You can retrieve the sources associated with a target using SOURCES
> properties.
> https://cmake.org/cmake/help/latest/prop_tgt/SOURCES.html
>
> then
> https://cmake.org/cmake/help/latest/command/foreach.html
> with
> https://cmake.org/cmake/help/latest/command/set_property.html
> or
> https://cmake.org/cmake/help/latest/command/set_source_files_properties.html
>
> for setting property:
> https://cmake.org/cmake/help/latest/prop_sf/COMPILE_DEFINITIONS.html
>
> on each source file.
>
> not sure why you want to do that you know that compiler already defined
> many "standard" macro for you, like __FILE__
> https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros
> ?
>
> --
> Eric
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake


--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Adding an individual define to each source file of a library

George PF
> > However, whatever I write into 'set_property()' - no APPEND, single entry,
> > not quoted - does not end up
> > on the compiler command line. Is there a type mismatch which is silently
> > ignored?
> >
>
> Or you are doing this in a directory which is not the one where the target
> is defined and from the doc:
> $ cmake --help-command set_property
>
> ...
> ``SOURCE``
>   Scope may name zero or more source files.  Note that source
>   file properties are visible only to targets added in the same
>   directory (CMakeLists.txt).
>
> ...

This is all in the same directory, full setup and test:

    % cmake --version
    cmake version 3.12.0   [..]
    % mkdir mylib && cd mylib
    mylib% touch file1.c file2.c
    mylib% cat > CMakeLists.txt

    cmake_minimum_required(VERSION 3.12)
   
    add_library(mylib SHARED file1.c file2.c)
   
    get_property(mysrcs TARGET mylib PROPERTY SOURCES)
    foreach(x IN LISTS mysrcs)
        message("at ${x}")
        set_property(SOURCE x APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")
    endforeach(x)
    # ^D

    mylib% mkdir build && cd build && cmake ..
    [..]
    at file1.c
    at file2.c
    -- Configuring done
    [..]
    mylibs/build% grep -r TEST1 . || echo no TEST1
    no TEST1

and 'make VERBOSE=1' also shows no extra -DTEST1 compiler arguments.
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Adding an individual define to each source file of a library

Eric Noulard


Le jeu. 23 août 2018 à 19:18, George PF <[hidden email]> a écrit :
> > However, whatever I write into 'set_property()' - no APPEND, single entry,
> > not quoted - does not end up
> > on the compiler command line. Is there a type mismatch which is silently
> > ignored?
> >
>
> Or you are doing this in a directory which is not the one where the target
> is defined and from the doc:
> $ cmake --help-command set_property
>
> ...
> ``SOURCE``
>   Scope may name zero or more source files.  Note that source
>   file properties are visible only to targets added in the same
>   directory (CMakeLists.txt).
>
> ...

This is all in the same directory, full setup and test:

    % cmake --version
    cmake version 3.12.0   [..]
    % mkdir mylib && cd mylib
    mylib% touch file1.c file2.c
    mylib% cat > CMakeLists.txt

    cmake_minimum_required(VERSION 3.12)

    add_library(mylib SHARED file1.c file2.c)

    get_property(mysrcs TARGET mylib PROPERTY SOURCES)
    foreach(x IN LISTS mysrcs)
        message("at ${x}")
        set_property(SOURCE x APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")

almost there but you forgot to take value of 'x'

set_property(SOURCE ${x} APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")

works for me.



 
    endforeach(x)
    # ^D

    mylib% mkdir build && cd build && cmake ..
    [..]
    at file1.c
    at file2.c
    -- Configuring done
    [..]
    mylibs/build% grep -r TEST1 . || echo no TEST1
    no TEST1

and 'make VERBOSE=1' also shows no extra -DTEST1 compiler arguments.
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake


--
Eric

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Adding an individual define to each source file of a library

George PF
>>     foreach(x IN LISTS mysrcs)
>>         message("at ${x}")
>>         set_property(SOURCE x APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")
>
> almost there but you forgot to take value of 'x'
>
> set_property(SOURCE ${x} APPEND PROPERTY COMPILE_DEFINITIONS "TEST1;TEST2;")
>
> works for me.

Thank you very much, now it indeed works.


FTR, extracted into a separate 'add_library_with_defs' function in the toplevel CMakeLists.txt


    function(add_library_with_defs libname libtype)
        math(EXPR endindex "${ARGC}-1")
        set(files)
        foreach(index RANGE 2 ${endindex})
            list(GET ARGV ${index} arg)
            list(APPEND files ${arg})
        endforeach()
   
        add_library("${libname}" "${libtype}" "${files}")
   
        get_property(mysrcs TARGET "${libname}" PROPERTY SOURCES)
        foreach(x IN LISTS mysrcs)
            # message("${libname}: at ${x}")
            set_property(SOURCE ${x} APPEND PROPERTY COMPILE_DEFINITIONS TEST1 TEST2)
        endforeach()
    endfunction()


so now it is simplified to: add_library_with_defs(mylib SHARED file1.c file2.c)
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake