CMake collect and process all source files in top directory

被刻印的时光 ゝ 提交于 2020-01-06 20:00:04

问题


I am trying to collect and process all source files in the top directory, to deal with the fact that variables are not passed to sub directories. I have cobbled together the following CMakeLists.txt files:

    cmake_minimum_required(VERSION 2.8)
project(main)
enable_language(Fortran)
enable_testing()

set (CMAKE_Fortran_COMPILER ifort)
set (CMAKE_Fortran_FLAGS " -g -C -fixed")
set (CMAKE_Fortran95_FLAGS " -openmp ")

# function to collect all the sources from sub-directories
# into a single list
function(add_sources)
  get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED)
  if(NOT is_defined)
    define_property(GLOBAL PROPERTY SRCS_LIST
      BRIEF_DOCS "List of source files"
      FULL_DOCS "List of source files to be compiled in one library")
  endif()
  # make absolute paths
  set(SRCS)
  foreach(s IN LISTS ARGN)
    if(NOT IS_ABSOLUTE "${s}")
      get_filename_component(s "${s}" ABSOLUTE)
    endif()
    list(APPEND SRCS "${s}")
  endforeach()
  # append to global list
  set_property(GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}")
endfunction(add_sources)



add_sources(SRCS main.f95)
add_subdirectory(sub)

# preprocess sources
set(PREP_SRCS)
get_property(SRCS GLOBAL PROPERTY SRCS_LIST)
foreach(s IN LISTS SRCS)
  file(RELATIVE_PATH rs "${CMAKE_CURRENT_SOURCE_DIR}" "${s}")
  string(REGEX REPLACE "f95$" "f" o "${CMAKE_CURRENT_BINARY_DIR}/${rs}")
  add_custom_command(
    OUTPUT "${o}"
    COMMAND ${CMAKE_COMMAND} -E copy "${s}" "${o}"
    DEPENDS "${s}"
    if(${s} MATCHES "f95$")
      set_source_files_properties( ${o} PROPERTIES 
     COMPILE_FLAGS ${CMAKE_Fortran95_FLAGS})
     endif(${s} MATCHES "f95$")
    COMMENT "Creating ${o}"
    VERBATIM
    )
  list(APPEND PREP_SRCS "${o}")
endforeach()

message(${PREP_SRCS})

add_executable(main ${PREP_SRCS})


SET_TARGET_PROPERTIES(main PROPERTIES LINKER_LANGUAGE Fortran)
SET_TARGET_PROPERTIES(main PROPERTIES 
LINK_FLAGS " ")

and in sub/

 add_sources(SRCS saxpy2.f95 saxpy_noomp.f)

which cmakes correctly:

[baron@ray:1034]$ cmake ..
-- The C compiler identification is AppleClang 7.0.2.7000181
-- The CXX compiler identification is AppleClang 7.0.2.7000181
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The Fortran compiler identification is Intel 15.0.1.20141022
-- Check for working Fortran compiler: /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort
-- Check for working Fortran compiler: /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort  -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort supports Fortran 90
-- Checking whether /opt/intel/composer_xe_2015.1.108/bin/intel64/ifort supports Fortran 90 -- yes
/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/SRCS/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/main.f/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/sub/SRCS/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/sub/saxpy2.f/Users/baron/teach/comps/ompstuff/new_new_cmake_test/build/sub/saxpy_noomp.f
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/baron/teach/comps/ompstuff/new_new_cmake_test/build
[baron@ray:1035]$ make
make[2]: *** No rule to make target `../)', needed by `sub/saxpy_noomp.f'.  Stop.
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
[baron@ray:1036]$ 

but obviously something subtle is missing. TIA

来源:https://stackoverflow.com/questions/34765296/cmake-collect-and-process-all-source-files-in-top-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!