0

I have a cmake project leveraging another cmake library project, the dir looks like this:

/CMakeList.txt 
/main.c
/.gitmodules
/patch/patch.c
/vendor/CMakeList.txt  
/vendor/vendor.c
/vendor/...

In /CMakeList.txt, I used add_subdirectory(vendor) to include/link vendor library to my own project.

In /vendor/CMakeList.txt, source file is added by FILE(GLOB SRC_VEN vendor.c ...)

I want to inject /patch/patch.c into vendor library without touching /vendor directory, which is a git submodule

Is it possible to use cmake to achieve this in my root CMakeList.txt?

Thanks!

jerry
  • 1,196
  • 2
  • 9
  • 21
  • how does `/vendor/CMakeList.txt` really look like? Doesn't it use a wildcard in `FILE(GLOB ...)`? What do you mean "without touching"? Could any files be changed automatically? – m.s. Jul 29 '15 at 12:25
  • 1
    Something like `target_sources(vendor_target PRIVATE "patch/patch.c")` after your `add_subdirectory(vendor)` call? But you need to know the target's name you want to inject into (`vendor_target` is just a placeholder). See [here](http://stackoverflow.com/questions/31538466/keeping-file-hierarchy-across-subdirectories-in-cmake) for more details. – Florian Jul 29 '15 at 12:34
  • Hi @m.s. thanks for the reply, yes `/vendor/CMakeLists.txt` uses wildcard, i mentioned 'without touch' vendor because I can modify `/vendor/CMakeLists.txt` a little bit, and `ignore=dirty` in my `.gitmodules` so that the change will not be committed – jerry Jul 29 '15 at 13:39
  • Thank you, @Florian ! just tested, target_sources works like a charm – jerry Jul 29 '15 at 13:52

1 Answers1

2

Turning my comment into an answer

If you want to inject sources into an existing target you can use target_sources() (introduced with CMake version 3.1).

In your case this would look something like:

/CMakeLists.txt

...
add_subdirectory(vendor)
target_sources(vendor_target PRIVATE "patch/patch.c") 

I put vendor_target as a placeholder. Please replace it with the real name for the target you want to inject the sources into.

For more details see e.g. here.

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149