Created
November 19, 2016 21:56
-
-
Save keharriso/eef0a412f2288c6a3d729b8e40149597 to your computer and use it in GitHub Desktop.
CMake module for minifying Lua code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # FindLuamin.cmake - Minify Lua code as a custom target | |
| # ------------------------------------------------------------------------------ | |
| # Provides: | |
| # | |
| # LUAMIN_MINIFY_TARGET TARGET WORKING_DIRECTORY OUT_DIR FILES... | |
| # TARGET - The name of the custom target | |
| # WORKING_DIRECTORY - The working directory for the luamin command | |
| # OUT_DIR - The directory to store minified .lua files | |
| # FILES... - The .lua files to minify (relative to WORKING_DIRECTORY) | |
| # | |
| # ------------------------------------------------------------------------------ | |
| # Copyright (c) Kevin Harrison 2016 | |
| # Released under the MIT License (https://opensource.org/licenses/MIT) | |
| find_program(LUAMIN_FOUND_luamin "luamin" PATHS ENV "PATH") | |
| if (NOT LUAMIN_FOUND_luamin) | |
| message(FATAL_ERROR "Failed to find luamin in PATH") | |
| endif (NOT LUAMIN_FOUND_luamin) | |
| function (LUAMIN_MINIFY_TARGET TARGET WORKING_DIRECTORY OUT_DIR) | |
| set(OUT "") | |
| foreach (lua_in ${ARGN}) | |
| file(RELATIVE_PATH relative_bin "${CMAKE_BINARY_DIR}" "${WORKING_DIRECTORY}/${lua_in}") | |
| file(RELATIVE_PATH relative_src "${CMAKE_SOURCE_DIR}" "${WORKING_DIRECTORY}/${lua_in}") | |
| string(LENGTH "${relative_bin}" rel_bin_len) | |
| string(LENGTH "${relative_src}" rel_src_len) | |
| if ("${rel_bin_len}" GREATER "${rel_src_len}") | |
| set(relative "${relative_src}") | |
| else ("${rel_bin_len}" GREATER "${rel_src_len}") | |
| set(relative "${relative_bin}") | |
| endif ("${rel_bin_len}" GREATER "${rel_src_len}") | |
| get_filename_component(dir "${lua_in}" DIRECTORY) | |
| get_filename_component(name "${lua_in}" NAME) | |
| set(lua_out "${OUT_DIR}/${dir}/${name}") | |
| list(APPEND OUT "${lua_out}") | |
| add_custom_command( | |
| OUTPUT "${lua_out}" | |
| COMMAND "${LUAMIN_FOUND_luamin}" "-f" "${lua_in}" > "${lua_out}" | |
| DEPENDS "${WORKING_DIRECTORY}/${lua_in}" | |
| WORKING_DIRECTORY "${WORKING_DIRECTORY}" | |
| COMMENT "Minifying ${relative}") | |
| endforeach (lua_in ${ARGN}) | |
| add_custom_target( | |
| "${TARGET}" | |
| ALL DEPENDS ${OUT}) | |
| endfunction (LUAMIN_MINIFY_TARGET TARGET WORKING_DIRECTORY OUT_DIR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment