Skip to content

Commit

Permalink
fix cmake plugin (create directory)
Browse files Browse the repository at this point in the history
simplified CMakeLists.txt file
  • Loading branch information
mhekkel committed Feb 10, 2024
1 parent 7f076a6 commit 10e8f6e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 98 deletions.
35 changes: 7 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
project(mrc VERSION 1.3.11)

include(CTest)
include(FetchContent)

set(CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 20)
Expand Down Expand Up @@ -61,19 +62,8 @@ if(WIN32)
message(STATUS "Using COFF spec: ${COFF_SPEC}")
endif()

find_package(libmcfp 1.2.4 QUIET)

if(NOT libmcfp_FOUND)
include(FetchContent)

FetchContent_Declare(
libmcfp
GIT_REPOSITORY https://github.com/mhekkel/libmcfp
GIT_TAG v1.2.4
)

FetchContent_MakeAvailable(libmcfp)
endif()
FetchContent_Declare(libmcfp GIT_REPOSITORY https://github.com/mhekkel/libmcfp GIT_TAG v1.2.4)
FetchContent_MakeAvailable(libmcfp)

include(VersionString)
write_version_header(${PROJECT_SOURCE_DIR}/src/)
Expand All @@ -90,19 +80,8 @@ target_link_libraries(mrc libmcfp::libmcfp)
target_link_libraries(mrc-bootstrap libmcfp::libmcfp)

if(BUILD_TESTING)
find_package(Catch2 3 QUIET)

if(NOT Catch2_FOUND)
Include(FetchContent)

FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.5.2 # or a later release
)

FetchContent_MakeAvailable(Catch2)
endif()
FetchContent_Declare(Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v2.13.9)
FetchContent_MakeAvailable(Catch2)

file(GLOB UNIT_TEST_RSRC LIST_DIRECTORIES true ${PROJECT_SOURCE_DIR}/rsrc/*)

Expand All @@ -112,7 +91,7 @@ if(BUILD_TESTING)
)

add_executable(mrc-unit-test ${PROJECT_SOURCE_DIR}/src/mrc-unit-test.cpp ${CMAKE_BINARY_DIR}/mrc-unit-test-rsrc.obj)
target_link_libraries(mrc-unit-test Catch2::Catch2WithMain)
target_link_libraries(mrc-unit-test Catch2::Catch2)

add_test(NAME unit-test COMMAND $<TARGET_FILE:mrc-unit-test> WORKING_DIRECTORY .)
endif()
Expand All @@ -127,4 +106,4 @@ else()
install(FILES doc/mrc.1 DESTINATION share/man/man1)
endif()

install(DIRECTORY example DESTINATION share/doc/mrc/)
install(DIRECTORY examples DESTINATION share/doc/mrc/)
133 changes: 68 additions & 65 deletions cmake/mrc-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer
# list of conditions and the following disclaimer
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Expand Down Expand Up @@ -53,39 +53,39 @@ Example usage:
#]=======================================================================]

find_program(MRC_EXECUTABLE
NAMES mrc
DOC "mrc executable"
NAMES mrc
DOC "mrc executable"
)

if(CMAKE_HOST_WIN32)
find_program(MRC_EXECUTABLE
NAMES mrc
PATHS $ENV{LOCALAPPDATA}
PATH_SUFFIXES mrc/bin
DOC "mrc executable"
)
find_program(MRC_EXECUTABLE
NAMES mrc
PATHS $ENV{LOCALAPPDATA}
PATH_SUFFIXES mrc/bin
DOC "mrc executable"
)
endif()

mark_as_advanced(MRC_EXECUTABLE)

# Retrieve the version number

execute_process(COMMAND ${MRC_EXECUTABLE} --version
OUTPUT_VARIABLE mrc_version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (mrc_version MATCHES "^mrc version [0-9]")
string(REPLACE "mrc version " "" MRC_VERSION_STRING "${mrc_version}")
OUTPUT_VARIABLE mrc_version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)

if(mrc_version MATCHES "^mrc version [0-9]")
string(REPLACE "mrc version " "" MRC_VERSION_STRING "${mrc_version}")
endif()

unset(mrc_version)

find_package(PackageHandleStandardArgs REQUIRED)
find_package_handle_standard_args(Mrc
REQUIRED_VARS MRC_EXECUTABLE
VERSION_VAR MRC_VERSION_STRING)
REQUIRED_VARS MRC_EXECUTABLE
VERSION_VAR MRC_VERSION_STRING)

# internal, create an ELF template file based on the current compiler flags and all

function(_mrc_create_elf_template _target)
try_compile(BUILD_OK
SOURCE_FROM_CONTENT my_code.cpp [[
Expand All @@ -94,7 +94,7 @@ int main() { return 0; }
]]
NO_CACHE
NO_LOG
COPY_FILE "${CMAKE_CURRENT_BINARY_DIR}/${_target}_rsrc_template.exe"
COPY_FILE "${_target}"
)

if(NOT BUILD_OK)
Expand All @@ -107,45 +107,43 @@ endfunction()
Add resources to a target::
mrc_target_resources(<target>
[RSRC_FILE <file>]
[DEPENDS_FILE <file>]
[VERBOSE]
[COFF_TYPE <AMD64|i386|ARM64>]
RESOURCES <file>...
)
mrc_target_resources(<target>
[RSRC_FILE <file>]
[DEPENDS_FILE <file>]
[VERBOSE]
[COFF_TYPE <AMD64|i386|ARM64>]
RESOURCES <file>...
)
This command will specify that resources should be added to
the target ``<target>``.
``RSRC_FILE``
Specify the resource file (object file) to create, default is
based on ``<target>``.
Specify the resource file (object file) to create, default is
based on ``<target>``.
``DEPENDS_FILE``
Specify the depends file to create, default is based on ``<target>``.
Specify the depends file to create, default is based on ``<target>``.
``VERBOSE``
Pass the --verbose to mrc so it will print out the list of files
added as resource and the path to be used to access them inside the
executable.
Pass the --verbose to mrc so it will print out the list of files
added as resource and the path to be used to access them inside the
executable.
``CREATE_ELF_TEMPLATE``
Create a small executable to be used as template for the ELF object
file generation. This is executable is built using the same compiler
settings as the final project and thus contains the correct ELF
header from which mrc can copy the necessary information.
``RESOURCES``
The files to pack into the resources of the executable. ``<file>``
here can be a file or a directory. Files will be added to the root
with their filename. Directories will be added recursively, if the
directory name ends with a slash character, the directory part of
the name will be stripped.
``CREATE_ELF_TEMPLATE``
Create a small executable to be used as template for the ELF object
file generation. This is executable is built using the same compiler
settings as the final project and thus contains the correct ELF
header from which mrc can copy the necessary information.
The files to pack into the resources of the executable. ``<file>``
here can be a file or a directory. Files will be added to the root
with their filename. Directories will be added recursively, if the
directory name ends with a slash character, the directory part of
the name will be stripped.
#]=======================================================================]

function(mrc_target_resources _target)

set(flags VERBOSE CREATE_ELF_TEMPLATE)
set(options COFF_TYPE RSRC_FILE DEPENDS_FILE)
set(sources RESOURCES)
Expand All @@ -164,21 +162,24 @@ function(mrc_target_resources _target)
endif()
endif()

set(_dir "${CMAKE_CURRENT_BINARY_DIR}/mrc-${_target}.dir")
file(MAKE_DIRECTORY ${_dir})

if(MRC_OPTION_RSRC_FILE)
set(RSRC_FILE ${MRC_OPTION_RSRC_FILE})
else()
set(RSRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/${_target}_rsrc.obj")
set(RSRC_FILE "${_dir}/rsrc.obj")
endif()

if(MRC_OTPION_RSRC_DEP_FILE)
set(RSRC_DEP_FILE "${MRC_OPTION_DEPENDS_FILE}")
else()
set(RSRC_DEP_FILE "${CMAKE_CURRENT_BINARY_DIR}/${_target}_rsrc.d")
set(RSRC_DEP_FILE "${_dir}/rsrc.d")
endif()

if(CMAKE_HOST_WIN32)
if(MRC_OPTION_COFF_TYPE)
set(COFF_SPEC "--coff=${MRC_OPTION_COFF_TYPE}")
list(APPEND OPTIONS "--coff=${MRC_OPTION_COFF_TYPE}")
else()
# Find out the processor type for the target
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
Expand All @@ -191,42 +192,45 @@ function(mrc_target_resources _target)
message(FATAL_ERROR "Unsupported or unknown processor type ${CMAKE_SYSTEM_PROCESSOR}")
endif()

set(COFF_SPEC "--coff=${COFF_TYPE}")
list(APPEND OPTIONS "--coff=${COFF_TYPE}")
endif()
endif()

if(${MRC_OPTION_VERBOSE})
list(APPEND MRC_OPTION_RESOURCES "--verbose")
list(APPEND OPTIONS "--verbose")
endif()

if(CMAKE_CROSSCOMPILING OR ${MRC_OPTION_CREATE_ELF_TEMPLATE})
_mrc_create_elf_template(${_target})
list(APPEND MRC_OPTION_RESOURCES "--elf-template=${CMAKE_CURRENT_BINARY_DIR}/${_target}_rsrc_template.exe")
set(TEMPLATE "${_dir}/template.exe")
_mrc_create_elf_template(${TEMPLATE})
list(APPEND OPTIONS "--elf-template=${TEMPLATE}")
endif()

cmake_policy(SET CMP0116 NEW)

# If we can use DEPFILE, use it.
# If we can use DEPFILE, use it.
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.21")
add_custom_target("mrc-depends-file_${_target}" ALL
add_custom_target("mrc-depends-file_${_target}" ALL
BYPRODUCTS ${RSRC_DEP_FILE}
COMMAND ${MRC_EXECUTABLE} -o ${RSRC_FILE} -d ${RSRC_DEP_FILE} ${MRC_OPTION_RESOURCES}
COMMAND ${MRC_EXECUTABLE} -o ${RSRC_FILE} -d ${RSRC_DEP_FILE}
${MRC_OPTION_RESOURCES}
VERBATIM)

add_custom_command(OUTPUT ${RSRC_FILE}
DEPFILE ${RSRC_DEP_FILE}
COMMAND ${MRC_EXECUTABLE} -o ${RSRC_FILE} ${COFF_SPEC} ${MRC_OPTION_RESOURCES}
COMMAND ${MRC_EXECUTABLE} -o ${RSRC_FILE} ${OPTIONS}
${MRC_OPTION_RESOURCES}
VERBATIM)
else()
message(STATUS "Not using dependency file since cmake version is too old")

add_custom_command(OUTPUT ${RSRC_FILE}
COMMAND ${MRC_EXECUTABLE} -o ${RSRC_FILE} ${COFF_SPEC} ${MRC_OPTION_RESOURCES}
COMMAND ${MRC_EXECUTABLE} -o ${RSRC_FILE} ${OPTIONS}
${MRC_OPTION_RESOURCES}
VERBATIM)

endif()

target_sources(${_target} PRIVATE ${RSRC_FILE})
target_sources(${_target} PRIVATE ${RSRC_FILE})
endfunction()

#[=======================================================================[.rst:
Expand All @@ -235,9 +239,8 @@ endfunction()
Write out the header file needed to use resources in a C++ application.
The argument specifies the file to create.
#]=======================================================================]

function(mrc_write_header _header)
execute_process(
COMMAND ${MRC_EXECUTABLE} --header --output ${_header}
)
execute_process(
COMMAND ${MRC_EXECUTABLE} --header --output ${_header}
)
endfunction()
17 changes: 13 additions & 4 deletions examples/simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.16)

list(PREPEND CMAKE_PREFIX_PATH ../../cmake)

# set the project name
project(mrc-user VERSION 1.0.0 LANGUAGES CXX)

Expand All @@ -18,7 +20,14 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
# The executable to create
add_executable(mrc-user ${CMAKE_CURRENT_SOURCE_DIR}/mrc-user.cpp)

# Add the hello.txt file as a resource, more files and/or directories
# can be specified here
mrc_target_resources(mrc-user CREATE_ELF_TEMPLATE VERBOSE
RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hello.txt)
# # Add the hello.txt file as a resource, more files and/or directories
# # can be specified here
# mrc_target_resources(mrc-user CREATE_ELF_TEMPLATE VERBOSE
# RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hello.txt)


add_library(rsrc STATIC)
set_target_properties(rsrc PROPERTIES LINKER_LANGUAGE "CXX")
mrc_target_resources(rsrc CREATE_ELF_TEMPLATE VERBOSE
RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hello.txt)
target_link_libraries(mrc-user PRIVATE rsrc)
5 changes: 4 additions & 1 deletion src/mrc-unit-test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <catch2/catch_test_macros.hpp>
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

#include "mrsrc.h"

#include <iostream>
#include <set>

#include <cstring>

TEST_CASE("test_1", "test_1")
{
mrsrc::rsrc r1("resource-1.txt");
Expand Down

0 comments on commit 10e8f6e

Please sign in to comment.