1

I need to use some makefile dependencies analog for cmake

hello: if_this_file_changes.cpp if_this_file_changes.txt
   run_some_command

basically , when I build it, I want to check if these files have changed (whenever there is a change in any of these files)? And if so, then call a command {some command like TOUCH or some script} And if files have not changed do nothing

cmake_minimum_required(VERSION 3.21)

project(ProjectName)

///////////////////////////////////
if this files have been changed 
    run command
////////////////////////
 

add_executable(hello source.cpp)

#simple example what I want to do

  • `add_custom_target` and `add_custom_command` are proper solutions for your problem. If they do not work for you, then describe that case with **more details**: what exact content of `CMakeLists.txt` do you use, what exact behavior you observe, etc. Description "it didnt work" is not the one with which we can help you. See also [ask]. – Tsyvarev Aug 13 '21 at 21:26
  • The problem is that I do not understand how to do that, no matter what wrong syntax i used, it doesn't work – Hayk Poghosyan Aug 13 '21 at 22:21
  • You could use e.g. [that question](https://stackoverflow.com/questions/2937128/cmake-add-custom-command-not-being-run) as the base for your attempts. – Tsyvarev Aug 13 '21 at 22:25

2 Answers2

0
hello: if_this_file_changes.cpp if_this_file_changes.txt
   run_some_command

If run_some_command creates hello:

add_custom_command(
    OUTPUT hello
    COMMAND run_some_command
    DEPENDS if_this_file_changes.cpp if_this_file_changes.txt
)

add_custom_target(hello DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/hello")

Otherwise if you want hello to be phony, you'll need to create a stamp file to get predictable update semantics:

add_custom_command(
    OUTPUT hello.stamp
    COMMAND run_some_command
    COMMAND "${CMAKE_COMMAND}" -E touch hello.stamp
    DEPENDS if_this_file_changes.cpp if_this_file_changes.txt
)

add_custom_target(hello DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/hello.stamp")
Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • It doesn't work, when I change if_this_file_changes.txt , nothing happens NO run_some_command re-run – Hayk Poghosyan Aug 14 '21 at 07:51
  • 1
    @HaykPoghosyan: Probably, you have modified some **other file** `if_this_file_changes.txt`, not the one which your custom command depends on. If unsure which file CMake uses as dependency, then use **absolute paths** for files in the DEPENDS option. For that you could use `${CMAKE_CURRENT_BINARY_DIR}` which refers to the build directory or `${CMAKE_CURRENT_SOURCE_DIR}` which refers to the source directory. – Tsyvarev Aug 14 '21 at 09:51
  • Yes it's works with ${CMAKE_CURRENT_BINARY_DIR}, I had to show the path explicitly.Thank you ALL – Hayk Poghosyan Aug 14 '21 at 10:08
  • @HaykPoghosyan - relative paths in `DEPENDS` are relative to the source directory, not the binary directory. This is why you needed an absolute path (also, you should have mentioned somewhere in your question that your text file was not a source file). – Alex Reinking Aug 14 '21 at 17:51
-1

A quick example of making all *.txt files a dependency

file(GLOB_RECURSE A_NAME *.txt)

add_custom_target(a_target_name COMMAND "your/command/here" "multi/commands/ok" DEPENDS ${A_NAME})

Your custom target now has dependencies, and if they change it cmake will do something.

StackAttack
  • 1,139
  • 1
  • 9
  • 15
  • 1
    A custom **target** (`add_custom_target`) runs a COMMAND **every time** it is built. For run a COMMAND **conditionally**, use custom **command** (`add_custom_command`) instead. – Tsyvarev Aug 13 '21 at 22:35
  • **Do not glob without `CONFIGURE_DEPENDS`** – Alex Reinking Aug 14 '21 at 00:12