Codechange: replace "magic" trick with simpler straight forward code
[openttd-github.git] / cmake / scripts / CreateGRF.cmake
blobeff9a7b95936307387aaba5c11d01f8e1ec6e268
1 cmake_minimum_required(VERSION 3.5)
4 # Create a single GRF file based on sprites/<grfname>.nfo and sprites/*.png
5 # files.
8 if(NOT NFORENUM_EXECUTABLE)
9     message(FATAL_ERROR "Script needs NFORENUM_EXECUTABLE defined")
10 endif()
11 if(NOT GRFCODEC_EXECUTABLE)
12     message(FATAL_ERROR "Script needs GRFCODEC_EXECUTABLE defined")
13 endif()
14 if(NOT GRFID_EXECUTABLE)
15     message(FATAL_ERROR "Script needs GRFID_EXECUTABLE defined")
16 endif()
17 if(NOT GRF_SOURCE_FOLDER)
18     message(FATAL_ERROR "Script needs GRF_SOURCE_FOLDER defined")
19 endif()
20 if(NOT GRF_BINARY_FILE)
21     message(FATAL_ERROR "Script needs GRF_BINARY_FILE defined")
22 endif()
24 # Remove the existing output so failures never go unnoticed
25 file(REMOVE ${GRF_BINARY_FILE} ${GRF_BINARY_FILE}.hash)
27 get_filename_component(GRF_SOURCE_FOLDER_NAME "${GRF_SOURCE_FOLDER}" NAME)
29 file(WRITE sprites/${GRF_SOURCE_FOLDER_NAME}.nfo "")
30 file(READ ${GRF_SOURCE_FOLDER}/${GRF_SOURCE_FOLDER_NAME}.nfo NFO_LINES)
31 # Replace ; with \;, and make a list out of this based on \n
32 string(REPLACE ";" "\\;" NFO_LINES "${NFO_LINES}")
33 string(REPLACE "\n" ";" NFO_LINES "${NFO_LINES}")
35 foreach(NFO_LINE IN LISTS NFO_LINES)
36     # Recover the ; that was really in the text (and not a newline)
37     string(REPLACE "\\;" ";" NFO_LINE "${NFO_LINE}")
39     if(NFO_LINE MATCHES "^#include")
40         string(REGEX REPLACE "^#include \"(.*)\"$" "\\1" INCLUDE_FILE ${NFO_LINE})
41         file(READ ${GRF_SOURCE_FOLDER}/${INCLUDE_FILE} INCLUDE_LINES)
42         file(APPEND sprites/${GRF_SOURCE_FOLDER_NAME}.nfo "${INCLUDE_LINES}")
43     else()
44         file(APPEND sprites/${GRF_SOURCE_FOLDER_NAME}.nfo "${NFO_LINE}\n")
45     endif()
46 endforeach()
48 execute_process(COMMAND ${NFORENUM_EXECUTABLE} -s sprites/${GRF_SOURCE_FOLDER_NAME}.nfo RESULT_VARIABLE RESULT)
49 if(RESULT)
50     if(NOT RESULT MATCHES "^[0-9]*$")
51         message(FATAL_ERROR "Failed to run NFORenum (${RESULT}), please check NFORENUM_EXECUTABLE variable")
52     endif()
53     message(FATAL_ERROR "NFORenum failed")
54 endif()
56 execute_process(COMMAND ${GRFCODEC_EXECUTABLE} -n -s -e -g2 -p1 ${GRF_SOURCE_FOLDER_NAME}.grf RESULT_VARIABLE RESULT)
57 if(RESULT)
58     if(NOT RESULT MATCHES "^[0-9]*$")
59         message(FATAL_ERROR "Failed to run GRFCodec (${RESULT}), please check GRFCODEC_EXECUTABLE variable")
60     endif()
61     message(FATAL_ERROR "GRFCodec failed")
62 endif()
64 execute_process(COMMAND ${GRFID_EXECUTABLE} -m ${GRF_SOURCE_FOLDER_NAME}.grf OUTPUT_VARIABLE GRFID_HASH RESULT_VARIABLE RESULT)
65 if(RESULT)
66     if(NOT RESULT MATCHES "^[0-9]*$")
67         message(FATAL_ERROR "Failed to run GRFID (${RESULT}), please check GRFID_EXECUTABLE variable")
68     endif()
69     message(FATAL_ERROR "GRFID failed")
70 endif()
72 file(WRITE ${GRF_BINARY_FILE}.hash ${GRFID_HASH})
74 # Copy build files back to the source directory.
75 execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${GRF_SOURCE_FOLDER_NAME}.grf ${GRF_BINARY_FILE})