How to use the program's exit status at compile time?

jiandingzhe

This question is subsequent to my previous one: How to integrate such kind of source generator into CMake build chain?

Currently, the C source file is generated from XS in this way:

set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${file_src_by_xs} PROPERTIES GENERATED 1)
add_custom_target(${file_src_by_xs}
    COMMAND ${XSUBPP_EXECUTABLE} ${XSUBPP_EXTRA_OPTIONS} ${lang_args} ${typemap_args} ${file_xs} >${CMAKE_CURRENT_BINARY_DIR}/${file_src_by_xs}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    DEPENDS ${file_xs} ${files_xsh} ${_XSUBPP_TYPEMAP_FILES}
    COMMENT "generating source from XS file ${file_xs}"
)

The GENERATED property let cmake don't check the existence of this source file at configure time, and add_custom_target let the xsubpp always re-run at each compile. The reason for always rerun is because xsubpp will generate an incomplete source file even if it fails, so there are possibility that the whole compiling continues with an incomplete source file.

I found it is time consuming to always re-run source generator and recompile it. So I want to have it re-run only when dependent XS files are modified. However, if I do so, the incomplete generated source file must be deleted.

So my question is: is there any way to remove the generated file, only when the program exit abnormally at compile time?

Or more generic: is there any way to run a command depending on another command's exit status at compile time?

jiandingzhe

Inspired by @Lindydancer's answer, I achieved the purpose by multiple COMMANDs in one target, and it don't need to write an external wrapper script.

set(source_file_ok ${source_file}.ok)
add_custom_command(
    OUTPUT ${source_file} ${source_file_ok}
    DEPENDS ${xs_file} ${xsh_files}
    COMMAND rm -f ${source_file_ok}
    COMMAND xsubpp ...... >${source_file}
    COMMAND touch ${source_file_ok}
)

add_library(${xs_lib} ${source_file})
add_dependencies(${xs_lib} ${source_file} ${source_file_ok})

The custom target has 3 commands. The OK file only exists when xsubpp is success, and this file is added as a dependency of the library. When xsubpp is not success, the dependency on the OK file will force the custom command to be run again.

The only flaw is cross-platform: not all OS have touch and rm, so the name of these two commands should be decided according to OS type.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to compile and use make command to install this certain program/tool?

分類Dev

How can I use std::make_tuple at compile time?

分類Dev

How to use GenericArray with a limited range of length that can be detected at compile time?

分類Dev

How to use GenericArray with a limited range of length that can be detected at compile time?

分類Dev

Bash pipeline's exit status differs in script

分類Dev

Example of compile-time substitutions worsening the program

分類Dev

what is the use of %.*s in this program

分類Dev

How is a vptr initialized at compile time?

分類Dev

Use variable "string" in compile-time commands

分類Dev

expect - how to exit with status code 1 if a command fails?

分類Dev

How to compile Saurik's Veency?

分類Dev

How to use mapGetters with modules and root's getters at the same time?

分類Dev

Is it possible to get the compiler to exit early, failing the build, if a compile time warning is raised?

分類Dev

How to enforce that a type implements a trait at compile time?

分類Dev

How to order types at compile-time?

分類Dev

how to check the size of an array during compile time

分類Dev

How to tell if expression is evaluated at compile time or runtime?

分類Dev

How to check at compile time if type is polymorhic

分類Dev

Exit program and open file

分類Dev

How to compile go program in 1.1.2 with dependencies compiled in 1.1.1?

分類Dev

How to compile program with _mm_clflushopt function? error: inlining failed

分類Dev

How can i solve the error when running xampp saying that the port/s are in use by another program?

分類Dev

How to pause program execution for some time?

分類Dev

How to exit script if external call exceeds time limit

分類Dev

macrodef not conveying the exit status correctly

分類Dev

mdmsetup returns exit status 1

分類Dev

How to compile for armv7s devices

分類Dev

How to fix ‘“ERROR: Command errored out with exit status 1:” when trying to install watchdog using pip

分類Dev

Need a fairly "rude" program exit

Related 関連記事

  1. 1

    How to compile and use make command to install this certain program/tool?

  2. 2

    How can I use std::make_tuple at compile time?

  3. 3

    How to use GenericArray with a limited range of length that can be detected at compile time?

  4. 4

    How to use GenericArray with a limited range of length that can be detected at compile time?

  5. 5

    Bash pipeline's exit status differs in script

  6. 6

    Example of compile-time substitutions worsening the program

  7. 7

    what is the use of %.*s in this program

  8. 8

    How is a vptr initialized at compile time?

  9. 9

    Use variable "string" in compile-time commands

  10. 10

    expect - how to exit with status code 1 if a command fails?

  11. 11

    How to compile Saurik's Veency?

  12. 12

    How to use mapGetters with modules and root's getters at the same time?

  13. 13

    Is it possible to get the compiler to exit early, failing the build, if a compile time warning is raised?

  14. 14

    How to enforce that a type implements a trait at compile time?

  15. 15

    How to order types at compile-time?

  16. 16

    how to check the size of an array during compile time

  17. 17

    How to tell if expression is evaluated at compile time or runtime?

  18. 18

    How to check at compile time if type is polymorhic

  19. 19

    Exit program and open file

  20. 20

    How to compile go program in 1.1.2 with dependencies compiled in 1.1.1?

  21. 21

    How to compile program with _mm_clflushopt function? error: inlining failed

  22. 22

    How can i solve the error when running xampp saying that the port/s are in use by another program?

  23. 23

    How to pause program execution for some time?

  24. 24

    How to exit script if external call exceeds time limit

  25. 25

    macrodef not conveying the exit status correctly

  26. 26

    mdmsetup returns exit status 1

  27. 27

    How to compile for armv7s devices

  28. 28

    How to fix ‘“ERROR: Command errored out with exit status 1:” when trying to install watchdog using pip

  29. 29

    Need a fairly "rude" program exit

ホットタグ

アーカイブ