Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions IncBinMacro.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# When I used INCBIN with cmake I have an error like this:
# "C:\Users\UserName\AppData\Local\Temp\ccgFslxv.s: Assembler messages:"
# "C:\Users\UserName\AppData\Local\Temp\ccgFslxv.s:2549: Error: file not found: FileName.txt" .
#
# This happens because the compiler cannot find the file in the build directory.
# With this macro you can automatically copy used files with a template [filename].incbin.[file extension]
# from directory [input_dir_absolute_path] to directory [output_dir_absolute_path].
# Сopied files will looks like [filename].[file extension].
#
# Usage:
# 1) move_incbin_to_destination([input_dir_absolute_path] [output_dir_absolute_path])
# This copy files from [input_dir_absolute_path] to [output_dir_absolute_path] and remove ".incbin" from them names.
# 2) move_incbin_to_destination([input_dir_absolute_path] [output_dir_absolute_path] RECURSE)
# This copy files from [input_dir_absolute_path]/[recursive all child folders] to
# [output_dir_absolute_path]/[recursive all child folders from input dir] and remove ".incbin" from them names.
#
# Typical usage is (including quotes):
# move_incbin_to_destination( "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}" RECURSE)
# You can find examples in folder "test" in the repository https://github.com/graphitemaster/incbin

MACRO(move_incbin_to_destination input_dir_absolute_path output_dir_absolute_path)
set(RECURSE_FLAG "OFF")

if(${ARGC} EQUAL 3)
if(${ARGV2} STREQUAL RECURSE)
set(RECURSE_FLAG ON)
else()
message(FATAL_ERROR "Unknow parameter ${ARGV2}")
endif()
endif(${ARGC} EQUAL 3)

if(${RECURSE_FLAG} STREQUAL "ON")
message("MOVE_INCBIN_TO_DESTINATION RECURSE from folder ${input_dir_absolute_path}")
file(GLOB_RECURSE INCBIN_FILES ${input_dir_absolute_path}/*.incbin*)
else()
message("MOVE_INCBIN_TO_DESTINATION from folder ${input_dir_absolute_path}")
file(GLOB INCBIN_FILES ${input_dir_absolute_path}/*.incbin*)
endif()

foreach(INCBIN_FILE ${INCBIN_FILES})

string(REGEX REPLACE "\\.incbin\\." "." NORMAL_FILE ${INCBIN_FILE})

string(REPLACE
${input_dir_absolute_path}
${output_dir_absolute_path}
NORMAL_FILE
${NORMAL_FILE})

message("\r\n"
" ${INCBIN_FILE} ==>\r\n"
" ${NORMAL_FILE}")

configure_file(${INCBIN_FILE} ${NORMAL_FILE} COPYONLY)
endforeach()

message("End of MOVE_INCBIN_TO_DESTINATION\r\n")
ENDMACRO()
15 changes: 15 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# For c examples
/asserts

# For CMake examples
build*
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
1 change: 1 addition & 0 deletions test/CMakeExample/1.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 0_1!
1 change: 1 addition & 0 deletions test/CMakeExample/2.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 0_2!
17 changes: 17 additions & 0 deletions test/CMakeExample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.1.0)

project(IncBinExample LANGUAGES C CXX)

include("../../IncBinMacro.cmake")

add_executable(example example.cpp)

# Read CMake output of this.
move_incbin_to_destination(
"${CMAKE_CURRENT_SOURCE_DIR}" # Folder with CMakeLists.txt.
"${CMAKE_CURRENT_BINARY_DIR}" # Build folder.
RECURSE
)

message("Binary dir: ${CMAKE_CURRENT_BINARY_DIR}")

44 changes: 44 additions & 0 deletions test/CMakeExample/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#define INCBIN_PREFIX txt_
#define out(x) std::cout << x << std::endl

#include <iostream>
#include <string>

#include "../../incbin.h"

INCBIN(1_, "1.txt");
INCBIN(2_, "2.txt");

INCBIN(f1_, "folder/1.txt");
INCBIN(f2_, "folder/2.txt");

INCBIN(f11_, "folder/1/1.txt");
INCBIN(f12_, "folder/1/2.txt");
INCBIN(f21_, "folder/2/1.txt");
INCBIN(f22_, "folder/2/2.txt");

int
main(int argc, char* argv[])
{
out((char*)txt_1_Data);
out((char*)txt_2_Data);

out((char*)txt_f1_Data);
out((char*)txt_f2_Data);

out((char*)txt_f11_Data);
out((char*)txt_f12_Data);
out((char*)txt_f21_Data);
out((char*)txt_f22_Data);

std::string dir = argv[0];
int splitter = std::max((signed int)dir.find_last_of("\\"),
(signed int)dir.find_last_of("/"));

dir = dir.substr(0, splitter);

std::cout << "Current dir: " << dir << std::endl;
std::cout << "Check it, all IncBin files are here!" << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions test/CMakeExample/folder/1.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 1_1!
1 change: 1 addition & 0 deletions test/CMakeExample/folder/1/1.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 2_1!
1 change: 1 addition & 0 deletions test/CMakeExample/folder/1/2.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 2_2!
1 change: 1 addition & 0 deletions test/CMakeExample/folder/2.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 1_2!
1 change: 1 addition & 0 deletions test/CMakeExample/folder/2/1.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 2_1!
1 change: 1 addition & 0 deletions test/CMakeExample/folder/2/2.incbin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello 2_2!