0

I'm trying to learn how to create archives with CMake. I wrote this piece of code:

cmake_minimum_required(VERSION 3.5.1)
project(hello)

message("Creating archieve in: " ${CMAKE_CURRENT_BINARY_DIR})
message("Source dir is: " ${CMAKE_CURRENT_SOURCE_DIR})

add_executable(hello main.cpp)


set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(create_tar ALL COMMAND
    ${CMAKE_COMMAND} -E tar "cfvz" "archieve.tgz")
add_dependencies(create_tar hello)

My project structure is like this: I have a main folder in which I have a CMakeLists.txt, a main.cpp and a build folder. I go into build and run the cmake file above by 'cmake ..' and then make. My archive is created in the build folder but as you can see I have specified that I want it in the CMAKE_CURRENT_SOURCE_DIR which is along with the main, cmakelists and build folder not in the actual build folder.

Please explain to me why is that happening and how can I make the archive be created in the CMAKE_CURRENT_SOURCE_DIR and not CMAKE_CURRENT_BINARY_DIR. Thanks.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Kennedy
  • 277
  • 2
  • 7
  • 21
  • `add_custom_target(create_tar ALL COMMAND ${CMAKE_COMMAND} -E tar "cfvz" "archieve.tgz" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")`? – Florian Feb 20 '18 at 11:28
  • @Florian thank you! I see the 'WORKING_DIRECTORY' sets where the output of that command will be stored. Also please tell me if possible how can I specify which files to archieve. By default I can see that it archieves the files that are in the same folder as the cmake file. How can I specify for example only archive files in directory named "Dir"? – Kennedy Feb 20 '18 at 11:53

1 Answers1

1

Turning my comment into an answer

That would be WORKING_DIRECTORY parameter of the add_custom_target() command:

add_custom_target(
    create_tar ALL 
    COMMAND ${CMAKE_COMMAND} -E tar cfvz "archieve.tgz" 
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

The CMAKE_ARCHIVE_OUTPUT_DIRECTORY is for changing the output directory of add_library(... STATIC ...)` targets.

And you can either run the cmake -E tar either the correct (sub-)directory by changing the WORKING_DIRECTORY accordingly (like "${CMAKE_CURRENT_SOURCE_DIR}/Dir") or give a list of files after -- command line option (see documentation of CMake Command Line Tool Mode).

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Awesome. Just one final question(s) :D. What does the 'ALL' option that we pass to add_custom_target do? I don't quite understand what the documentation says. Also why do we need to specify ${CMAKE_COMAND} } -E and not just write 'tar blah blah'. Thanks. – Kennedy Feb 20 '18 at 12:50
  • Also: Why do we need to specify arguments for the tar command between ""? I noticed it works even if I write it normally like I would in the terminal like this: ' COMMAND tar cfvz archieve.tgz ${CMAKE_CURRENT_SOURCE_DIR}/include ' Thanks. – Kennedy Feb 20 '18 at 12:58
  • @Kennedy The `ALL` option just means the build step is executed if you just call e.g. `make`. If you remove the `ALL` option you will need to explicitly trigger the build step by calling e.g. `make create_tar`. – Florian Feb 20 '18 at 19:30
  • @Kennedy If you directly use `tar` then the `tar` program has to be on your PC and in your `PATH` environment. Since CMake is build for cross-platform support (that's where the `C` in its name is coming from) it is better to use CMake build in Command Line Mode and call `cmake -E tar` (works also on e.g. Windows). – Florian Feb 20 '18 at 19:33
  • @Kennedy The quotes are not needed for the parameters. So for more clarity I've removed them in my answer. About quoting in CMake see ["cmake: when to quote variables?"](https://stackoverflow.com/questions/35847655/cmake-when-to-quote-variables). – Florian Feb 20 '18 at 19:35