CMakeLists.txt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. cmake_minimum_required (VERSION 2.6)
  2. project (ExternalOBJ)
  3. if(APPLE)
  4. # set _CMAKE_OSX_MACHINE to umame -m
  5. exec_program(uname ARGS -m OUTPUT_VARIABLE _CMAKE_OSX_MACHINE)
  6. # check for Power PC and change to ppc
  7. if("${_CMAKE_OSX_MACHINE}" MATCHES "Power")
  8. set(_CMAKE_OSX_MACHINE ppc)
  9. endif()
  10. set(CMAKE_OSX_ARCHITECTURES ${_CMAKE_OSX_MACHINE})
  11. endif()
  12. # Build the external object file.
  13. try_compile(EXTERNAL_OBJECT_BUILT
  14. ${ExternalOBJ_BINARY_DIR}/Object
  15. ${ExternalOBJ_SOURCE_DIR}/Object
  16. Object
  17. external
  18. OUTPUT_VARIABLE OUTPUT
  19. )
  20. if(EXTERNAL_OBJECT_BUILT)
  21. message(
  22. "Building external_object.cxx succeeded with the following output:\n"
  23. "[${OUTPUT}]"
  24. )
  25. else()
  26. message(FATAL_ERROR
  27. "Building external_object.cxx failed with the following output:\n"
  28. "[${OUTPUT}]"
  29. )
  30. endif()
  31. # Find the external object file.
  32. set(DIR ${ExternalOBJ_BINARY_DIR}/Object)
  33. file(GLOB_RECURSE EXTERNAL_OBJECT
  34. "${DIR}/external_object*${CMAKE_CXX_OUTPUT_EXTENSION}")
  35. if(EXTERNAL_OBJECT)
  36. list (GET EXTERNAL_OBJECT 0 EXTERNAL_OBJECT)
  37. message("Found \"${EXTERNAL_OBJECT}\".")
  38. else()
  39. message(FATAL_ERROR "Could not find external object.")
  40. endif()
  41. # Test creation of external objects by custom commands.
  42. set(CUSTOM_OBJECT
  43. ${CMAKE_CURRENT_BINARY_DIR}/custom_object${CMAKE_C_OUTPUT_EXTENSION})
  44. add_custom_command(
  45. OUTPUT ${CUSTOM_OBJECT}
  46. COMMAND ${CMAKE_COMMAND} -E copy ${EXTERNAL_OBJECT} ${CUSTOM_OBJECT}
  47. DEPENDS ${EXTERNAL_OBJECT}
  48. )
  49. message("${EXTERNAL_OBJECT}")
  50. # Build an executable using the external object file.
  51. add_executable(ExternalOBJ executable.cxx ${CUSTOM_OBJECT})
  52. # A bug showed up in VS2010 where an object file that was
  53. # part of a custom command output worked, but ones that were
  54. # not didn't work. So, repeat the executable using the object
  55. # directly and not from the output of the copy.
  56. add_executable(ExternalOBJ2 executable.cxx ${EXTERNAL_OBJECT})
  57. add_subdirectory(Sub)