Browse Source

Generate actionlist.h by CMake

interpolate
Michael Uleysky 1 year ago
parent
commit
a73f996ce5
  1. 53
      scripts/genactionslist
  2. 35
      src/CMakeLists.txt

53
scripts/genactionslist

@ -1,53 +0,0 @@
#!/bin/bash
DIR="$(dirname $0)/../include"
COMPILER="$1"
OUT="$2"
shift
shift
declare -a EXCLUDE_ACTIONS=( $@ )
included()
{
local name="$1"
local i=0
while [ $i -lt ${#EXCLUDE_ACTIONS[@]} ]; do
if [ "$name" == "${EXCLUDE_ACTIONS[$i]}" ]; then
return 1
fi
let i++
done
return 0
}
if [ -z "$COMPILER" ]; then
exit 1
fi
actlist=""
echo "">"$OUT"
cdir="$(pwd)"
pushd "$DIR">/dev/null
for f in ../actions/action*.h ../actions-add/action*.h; do
if [ -f "$f" ]; then
# echo Compiler=$COMPILER
# echo Exclude list=${EXCLUDE_ACTIONS[@]}
name="$($COMPILER -E -DGENACTIONLIST "$f" 2>/dev/null| grep "ADD ACTION CLASS: " | sed "s/.*: //")"
name=${name//;/}
if included "$name"; then
pushd "$cdir">/dev/null
echo "#include \"$f\"">>"$OUT"
actlist+=" Action$name"
popd>/dev/null
fi
fi
done
actlist=${actlist## }
actlist=${actlist%% }
actlist=${actlist// /,}
pushd "$cdir">/dev/null
echo "#define ACTLIST $actlist">>"$OUT"
popd>/dev/null

35
src/CMakeLists.txt

@ -1,18 +1,41 @@
set(EXENAME odm)
set(ACTIONLIST ../include/actionlist.h)
set(GENALIST ${CMAKE_SOURCE_DIR}/scripts/genactionslist)
set(ACTIONLISTINC ${CMAKE_CURRENT_BINARY_DIR}/../include/actionlist.h) # Include actions files and define the actions classes list
find_library(netcdf netcdf REQUIRED)
find_package(OpenMP REQUIRED)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
file(GLOB srcs CONFIGURE_DEPENDS *.cpp)
add_executable(${EXENAME} ${srcs} ${ACTIONLIST})
add_executable(${EXENAME} ${srcs} ${ACTIONLISTINC})
target_include_directories(${EXENAME} PRIVATE ../michlib/michlib ${CMAKE_CURRENT_BINARY_DIR}/../include)
file(GLOB actfiles CONFIGURE_DEPENDS ../actions/action*.h ../actions-add/action*.h)
add_custom_command(OUTPUT ${ACTIONLIST} COMMAND ${GENALIST} ARGS "${CMAKE_CXX_COMPILER} -I$<JOIN:$<TARGET_PROPERTY:${EXENAME},INCLUDE_DIRECTORIES>, -I>" ${ACTIONLIST} ${EXCLUDE_ACTIONS} DEPENDS ${GENALIST} ${actfiles})
target_link_libraries(${EXENAME} ${linker_options} ${netcdf} OpenMP::OpenMP_CXX teos)
set_target_properties(${EXENAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
install(TARGETS ${EXENAME})
# Begin generation of actions list
get_target_property(INCS ${EXENAME} INCLUDE_DIRECTORIES)
list(TRANSFORM INCS PREPEND "-I")
separate_arguments(excluded UNIX_COMMAND "${EXCLUDE_ACTIONS}")
file(GLOB actfiles RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS ../actions/action*.h ../actions-add/action*.h)
foreach(actfile ${actfiles})
execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${INCS} -E -DGENACTIONLIST ${actfile} OUTPUT_VARIABLE outfull ERROR_VARIABLE err WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX MATCH "ADD ACTION CLASS: [^ ;\n]+" outstr "${outfull}")
string(REGEX REPLACE ".+: " "" act "${outstr}")
if(NOT (("${act}" EQUAL "") OR ("${act}" IN_LIST excluded)))
message("Action: ${act} in file ${actfile}")
list(APPEND actlist ${act})
list(APPEND actfilelist ${actfile})
endif()
endforeach(actfile)
list(TRANSFORM actlist PREPEND "Action")
string(REPLACE ";" "," actclist "${actlist}")
file(WRITE ${ACTIONLISTINC} "#define ACTLIST ${actclist}\n")
foreach(actfile ${actfilelist})
file(APPEND ${ACTIONLISTINC} "#include \"${actfile}\"\n")
endforeach(actfile)
# End generation of actions list

Loading…
Cancel
Save