Holtorf
Holtorf

Reputation: 1491

How to call a custom cmake script only when files are updated?

I want to run an JavaScript minifier only when my JavaScript files are updated. I know how to do this in make, but not in CMakelist. Currently I have a script that minifies the JavaScript files, but it runs any time any files update.

add_custom_target(MINI-JS ALL DEPENDS ${JS_FILES}
    COMMAND rm -f ../projectv/js/all-min.js
    COMMAND cat ${JS_FILES} | jsmin >> ../projectv/js/all-min.js
)

Edit: Tsyvarev's comment seems to suggest this:

add_custom_command(OUTPUT ../projectv/js/all-min.js
    DEPENDS ${JS_FILES}
    COMMAND rm -f ../projectv/js/all-min.js
    COMMAND cat ${JS_FILES} | jsmin >> ../projectv/js/all-min.js
)
add_custom_target(MINI-JS ALL DEPENDS ../projectv/js/all-min.js)

This never updates, even when the JavaScript files change. Am I missing something?

Upvotes: 0

Views: 668

Answers (1)

user15308163
user15308163

Reputation:

The code below should work. In my example I'm using CMake command line tool -E mode. This way the custom command is more portable. My example relies on 3.17 though.


# SUPER IMPORTANT: Always specify absolute paths!
set(foobar "${CMAKE_CURRENT_LIST_DIR}../projectv/js/all-min.js")

add_custom_command(
    OUTPUT 
       # This is the file your custom command will create
       ${foobar}

    DEPENDS
       # These are all the files you need to run your custom command
       ${JS_FILES}

    COMMAND
       COMMAND ${CMAKE_COMMAND} -E rm ${foobar}
       COMMAND ${CMAKE_COMMAND} -E cat ${JS_FILES} | jsmin >> ${foobar}
)

add_custom_target(MINI-JS ALL DEPENDS ${foobar})

Upvotes: 4

Related Questions