try_run.rst 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. try_run
  2. -------
  3. Try compiling and then running some code.
  4. ::
  5. try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR
  6. bindir srcfile [CMAKE_FLAGS <Flags>]
  7. [COMPILE_DEFINITIONS <flags>]
  8. [COMPILE_OUTPUT_VARIABLE comp]
  9. [RUN_OUTPUT_VARIABLE run]
  10. [OUTPUT_VARIABLE var]
  11. [ARGS <arg1> <arg2>...])
  12. Try compiling a srcfile. Return TRUE or FALSE for success or failure
  13. in COMPILE_RESULT_VAR. Then if the compile succeeded, run the
  14. executable and return its exit code in RUN_RESULT_VAR. If the
  15. executable was built, but failed to run, then RUN_RESULT_VAR will be
  16. set to FAILED_TO_RUN. COMPILE_OUTPUT_VARIABLE specifies the variable
  17. where the output from the compile step goes. RUN_OUTPUT_VARIABLE
  18. specifies the variable where the output from the running executable
  19. goes.
  20. For compatibility reasons OUTPUT_VARIABLE is still supported, which
  21. gives you the output from the compile and run step combined.
  22. Cross compiling issues
  23. When cross compiling, the executable compiled in the first step
  24. usually cannot be run on the build host. try_run() checks the
  25. CMAKE_CROSSCOMPILING variable to detect whether CMake is in
  26. crosscompiling mode. If that's the case, it will still try to compile
  27. the executable, but it will not try to run the executable. Instead it
  28. will create cache variables which must be filled by the user or by
  29. presetting them in some CMake script file to the values the executable
  30. would have produced if it had been run on its actual target platform.
  31. These variables are RUN_RESULT_VAR (explanation see above) and if
  32. RUN_OUTPUT_VARIABLE (or OUTPUT_VARIABLE) was used, an additional cache
  33. variable RUN_RESULT_VAR__COMPILE_RESULT_VAR__TRYRUN_OUTPUT.This is
  34. intended to hold stdout and stderr from the executable.
  35. In order to make cross compiling your project easier, use try_run only
  36. if really required. If you use try_run, use RUN_OUTPUT_VARIABLE (or
  37. OUTPUT_VARIABLE) only if really required. Using them will require
  38. that when crosscompiling, the cache variables will have to be set
  39. manually to the output of the executable. You can also "guard" the
  40. calls to try_run with if(CMAKE_CROSSCOMPILING) and provide an
  41. easy-to-preset alternative for this case.
  42. Set variable CMAKE_TRY_COMPILE_CONFIGURATION to choose a build
  43. configuration.