Skip to content

Commit

Permalink
cmake: more granular script&support installation
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMatula committed Dec 2, 2020
1 parent 6adef43 commit b7e1f1c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 98 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ endif()

add_subdirectory(deps)
cond_add_subdirectory(doc RETDEC_DOC)
cond_add_subdirectory(scripts RETDEC_ENABLE_SCRIPTS)
add_subdirectory(scripts)
add_subdirectory(src)
cond_add_subdirectory(support RETDEC_ENABLE_SUPPORT)
add_subdirectory(support)
add_subdirectory(tests)

# Create config version file.
Expand Down
26 changes: 20 additions & 6 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ macro(set_if_all_set res)
set(${res} ${r})
endmacro()

# scripts
set_if_at_least_one_set(RETDEC_ENABLE_SCRIPTS RETDEC_ENABLE_ALL)

# support
set_if_at_least_one_set(RETDEC_ENABLE_SUPPORT RETDEC_ENABLE_ALL)

# src

# Executables - targets are not used anywhere.
Expand Down Expand Up @@ -502,3 +496,23 @@ set_if_at_least_one_set(RETDEC_ENABLE_YARAMOD
RETDEC_ENABLE_IDR2PAT
RETDEC_ENABLE_PAT2YARA
RETDEC_ENABLE_PATTERNGEN)

# Support

set_if_at_least_one_set(RETDEC_ENABLE_SUPPORT_ORDINALS
RETDEC_ENABLE_BIN2LLVMIR)

set_if_at_least_one_set(RETDEC_ENABLE_SUPPORT_TYPES
RETDEC_ENABLE_BIN2LLVMIR)

set_if_at_least_one_set(RETDEC_ENABLE_SUPPORT_YARA_SIGNSRCH
RETDEC_ENABLE_RETDEC
RETDEC_ENABLE_FILEINFO)

set_if_at_least_one_set(RETDEC_ENABLE_SUPPORT_YARA_TOOLS
RETDEC_ENABLE_RETDEC
RETDEC_ENABLE_CPDETECT
RETDEC_ENABLE_FILEINFO)

set_if_at_least_one_set(RETDEC_ENABLE_SUPPORT_YARA_STATIC_CODE
RETDEC_ENABLE_RETDEC)
56 changes: 36 additions & 20 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@

install(
PROGRAMS "retdec-archive-decompiler.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
if(RETDEC_ENABLE_RETDEC_DECOMPILER)
install(
PROGRAMS "retdec-archive-decompiler.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
endif()

install(
PROGRAMS "retdec-fileinfo.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
if(RETDEC_ENABLE_FILEINFO)
install(
PROGRAMS "retdec-fileinfo.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
endif()

if(RETDEC_TESTS)
install(
Expand All @@ -16,15 +20,27 @@ if(RETDEC_TESTS)
)
endif()

install(
PROGRAMS "retdec-signature-from-library-creator.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
install(
PROGRAMS "retdec-unpacker.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
install(
PROGRAMS "retdec-utils.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
if(RETDEC_ENABLE_ALL)
install(
PROGRAMS "retdec-signature-from-library-creator.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
endif()

if(RETDEC_ENABLE_UNPACKERTOOL)
install(
PROGRAMS "retdec-unpacker.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
endif()

if(RETDEC_ENABLE_RETDEC_DECOMPILER
OR RETDEC_ENABLE_FILEINFO
OR RETDEC_TESTS
OR RETDEC_ENABLE_ALL
OR RETDEC_ENABLE_UNPACKERTOOL)
install(
PROGRAMS "retdec-utils.py"
DESTINATION ${RETDEC_INSTALL_BIN_DIR}
)
endif()
153 changes: 83 additions & 70 deletions support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,105 +11,118 @@ set(YARAC_VERSION_PATH "${SUPPORT_TARGET_DIR}/version-yarac.txt")

# Clean the support target directory if YARA compilation flag changed.
#
check_if_variable_changed(RETDEC_COMPILE_YARA CHANGED)
if(CHANGED)
message(STATUS "Support: YARA rules compilation flag changed -> cleaning RetDec support.")
FILE(REMOVE_RECURSE "${SUPPORT_TARGET_DIR}")
if(RETDEC_ENABLE_SUPPORT_YARA_SIGNSRCH OR RETDEC_ENABLE_SUPPORT_YARA_TOOLS OR RETDEC_ENABLE_SUPPORT_YARA_STATIC_CODE)
check_if_variable_changed(RETDEC_COMPILE_YARA CHANGED)
if(CHANGED)
message(STATUS "Support: YARA rules compilation flag changed -> cleaning RetDec support.")
FILE(REMOVE_RECURSE "${SUPPORT_TARGET_DIR}")
endif()
endif()

# Clean the support target directory if yarac version changed.
# Also clean it if there is no yarac version file - we cannot be sure what
# version was used in such a case.
#
install(CODE "
if(EXISTS \"${YARAC_VERSION_PATH}\")
file(READ \"${YARAC_VERSION_PATH}\" YARAC_OLD_VERSION)
execute_process(
COMMAND \"${YARAC_PATH}\" --version
OUTPUT_VARIABLE YARAC_CURRENT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(YARAC_OLD_VERSION STREQUAL YARAC_CURRENT_VERSION)
message(STATUS \"Up-to-date: yarac version '\${YARAC_OLD_VERSION}'\")
else()
message(STATUS \"Previously used yarac version '\${YARAC_OLD_VERSION}' \
does not match the current version '\${YARAC_CURRENT_VERSION}' \
-> clean the support directory\")
if(RETDEC_ENABLE_SUPPORT_YARA_SIGNSRCH OR RETDEC_ENABLE_SUPPORT_YARA_TOOLS OR RETDEC_ENABLE_SUPPORT_YARA_STATIC_CODE)
install(CODE "
if(EXISTS \"${YARAC_VERSION_PATH}\")
file(READ \"${YARAC_VERSION_PATH}\" YARAC_OLD_VERSION)
execute_process(
COMMAND \"${YARAC_PATH}\" --version
OUTPUT_VARIABLE YARAC_CURRENT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(YARAC_OLD_VERSION STREQUAL YARAC_CURRENT_VERSION)
message(STATUS \"Up-to-date: yarac version '\${YARAC_OLD_VERSION}'\")
else()
message(STATUS \"Previously used yarac version '\${YARAC_OLD_VERSION}' \
does not match the current version '\${YARAC_CURRENT_VERSION}' \
-> clean the support directory\")
FILE(REMOVE_RECURSE \"${SUPPORT_TARGET_DIR}\")
endif()
elseif(EXISTS \"${SUPPORT_TARGET_DIR}\")
message(STATUS \"yarac version does not exist \
-> clean the support directory\")
FILE(REMOVE_RECURSE \"${SUPPORT_TARGET_DIR}\")
else()
# Support directory itself does not yet exist.
endif()
elseif(EXISTS \"${SUPPORT_TARGET_DIR}\")
message(STATUS \"yarac version does not exist \
-> clean the support directory\")
FILE(REMOVE_RECURSE \"${SUPPORT_TARGET_DIR}\")
else()
# Support directory itself does not yet exist.
endif()
")
")
endif()

# Get and install external support package from the retdec-support repository.
# This step may erase the entire target support directory, so it needs to go
# first.
#
install(CODE "
execute_process(
# -u = unbuffered -> print debug messages right away.
COMMAND \"${PYTHON_EXECUTABLE}\" -u \"${PROJECT_SOURCE_DIR}/support/install-share.py\" \"${CMAKE_INSTALL_PREFIX}\" \"${SUPPORT_PKG_URL}\" \"${SUPPORT_PKG_SHA256}\" \"${SUPPORT_PKG_VERSION}\"
RESULT_VARIABLE INSTALL_SHARE_RES
)
if(INSTALL_SHARE_RES)
message(FATAL_ERROR \"RetDec share directory installation FAILED\")
endif()
")
if(RETDEC_ENABLE_SUPPORT_YARA_STATIC_CODE)
install(CODE "
execute_process(
# -u = unbuffered -> print debug messages right away.
COMMAND \"${PYTHON_EXECUTABLE}\" -u \"${PROJECT_SOURCE_DIR}/support/install-share.py\" \"${CMAKE_INSTALL_PREFIX}\" \"${SUPPORT_PKG_URL}\" \"${SUPPORT_PKG_SHA256}\" \"${SUPPORT_PKG_VERSION}\"
RESULT_VARIABLE INSTALL_SHARE_RES
)
if(INSTALL_SHARE_RES)
message(FATAL_ERROR \"RetDec share directory installation FAILED\")
endif()
")
endif()

# Write currently used yarac version to the support directory.
#
install(CODE "
execute_process(
COMMAND \"${YARAC_PATH}\" --version
OUTPUT_VARIABLE YARAC_CURRENT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS \"yarac version '\${YARAC_CURRENT_VERSION}' \
written to '${YARAC_VERSION_PATH}'\")
file(WRITE \"${YARAC_VERSION_PATH}\" \"\${YARAC_CURRENT_VERSION}\")
")
if(RETDEC_ENABLE_SUPPORT_YARA_SIGNSRCH OR RETDEC_ENABLE_SUPPORT_YARA_TOOLS OR RETDEC_ENABLE_SUPPORT_YARA_STATIC_CODE)
install(CODE "
execute_process(
COMMAND \"${YARAC_PATH}\" --version
OUTPUT_VARIABLE YARAC_CURRENT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS \"yarac version '\${YARAC_CURRENT_VERSION}' \
written to '${YARAC_VERSION_PATH}'\")
file(WRITE \"${YARAC_VERSION_PATH}\" \"\${YARAC_CURRENT_VERSION}\")
")
endif()

# Install ordinal number databases.
#
install(
DIRECTORY ordinals
DESTINATION ${SUPPORT_TARGET_DIR}/
)
if(RETDEC_ENABLE_SUPPORT_ORDINALS)
install(
DIRECTORY ordinals
DESTINATION ${SUPPORT_TARGET_DIR}/
)
endif()

# Install yara patterns.
#
# Nothing - these are installed by the following Python script.

# Install YARA rules for tools detection.
#
set(YARA_INSTALL_PY "${PROJECT_SOURCE_DIR}/support/install-yara.py")

install(CODE "
execute_process(
COMMAND \"${PYTHON_EXECUTABLE}\" -u \"${YARA_INSTALL_PY}\"
\"${YARAC_PATH}\"
\"${SUPPORT_TARGET_DIR}\"
\"${PROJECT_SOURCE_DIR}/support/yara_patterns\"
${RETDEC_COMPILE_YARA}
RESULT_VARIABLE INSTALL_YARA_RES
)
if(INSTALL_YARA_RES)
message(FATAL_ERROR \"YARA tool signatures installation FAILED\")
endif()
")
if(RETDEC_ENABLE_SUPPORT_YARA_SIGNSRCH OR RETDEC_ENABLE_SUPPORT_YARA_TOOLS)
set(YARA_INSTALL_PY "${PROJECT_SOURCE_DIR}/support/install-yara.py")
install(CODE "
execute_process(
COMMAND \"${PYTHON_EXECUTABLE}\" -u \"${YARA_INSTALL_PY}\"
\"${YARAC_PATH}\"
\"${SUPPORT_TARGET_DIR}\"
\"${PROJECT_SOURCE_DIR}/support/yara_patterns\"
${RETDEC_COMPILE_YARA}
RESULT_VARIABLE INSTALL_YARA_RES
)
if(INSTALL_YARA_RES)
message(FATAL_ERROR \"YARA tool signatures installation FAILED\")
endif()
")
endif()

# Install retdec config.
#
set(RETDEC_DECOMPILER_CONFIG "decompiler-config.json")
install(
FILES ${RETDEC_DECOMPILER_CONFIG}
DESTINATION ${RETDEC_INSTALL_DATA_DIR}
)
if(RETDEC_ENABLE_RETDEC_DECOMPILER)
set(RETDEC_DECOMPILER_CONFIG "decompiler-config.json")
install(
FILES ${RETDEC_DECOMPILER_CONFIG}
DESTINATION ${RETDEC_INSTALL_DATA_DIR}
)
endif()

# Is this necessary?
set(CMAKE_INSTALL_MESSAGE ALWAYS)

0 comments on commit b7e1f1c

Please sign in to comment.