Skip to content

Commit

Permalink
Emscripten: Output and display build info
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Jun 11, 2024
1 parent 96a2aef commit 54aebb1
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
87 changes: 87 additions & 0 deletions platforms/emscripten/cmake/autorevision_externpostjs.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
cmake_minimum_required(VERSION 3.5...3.30)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

# Automatically update the version / build info in the WZ extern-postjs.js file
#
# Required input defines:
# - CACHEFILE: the path to the autorevision.cache file generated for the build
# - PROJECT_ROOT: the path the project root (${PROJECT_SOURCE_DIR})
# - TEMPLATE_FILE: the full filename + path for the input extern-postjs.js.in template file
# - OUTPUT_FILE: the full filename + path for the output extern-postjs.js file
#

if(NOT DEFINED CACHEFILE OR "${CACHEFILE}" STREQUAL "")
message( FATAL_ERROR "Missing required input define: CACHEFILE" )
endif()
if(NOT DEFINED PROJECT_ROOT OR "${PROJECT_ROOT}" STREQUAL "")
message( FATAL_ERROR "Missing required input define: PROJECT_ROOT" )
endif()
if(NOT DEFINED TEMPLATE_FILE OR "${TEMPLATE_FILE}" STREQUAL "")
message( FATAL_ERROR "Missing required input define: TEMPLATE_FILE" )
endif()
if(NOT DEFINED OUTPUT_FILE OR "${OUTPUT_FILE}" STREQUAL "")
message( FATAL_ERROR "Missing required input define: OUTPUT_FILE" )
endif()

#################################

execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++Get build revision info from: ${CACHEFILE}")

# Attempt to get version information from the current build's autorevision cache. Fail if cache is not present.
execute_process(COMMAND ${CMAKE_COMMAND} "-DCACHEFILE=${CACHEFILE}" -DSKIPUPDATECACHE=1 -DCACHEFORCE=1 -DVAROUT=1 -P "${PROJECT_ROOT}/build_tools/autorevision.cmake"
WORKING_DIRECTORY "${PROJECT_ROOT}"
OUTPUT_VARIABLE autorevision_info
OUTPUT_STRIP_TRAILING_WHITESPACE
)

include("${PROJECT_ROOT}/build_tools/autorevision_helpers.cmake")

# Import the autorevision values into the current scope
cmakeSetAutorevisionValues("${autorevision_info}")

unset(EMSCRIPTEN_WZ_VERSIONSTRING)
unset(EMSCRIPTEN_WZ_GITCOMMIT)

##################################
# Extract a version string
# EMSCRIPTEN_WZ_VERSIONSTRING

unset(DID_EXTRACT_VERSION)

if(DEFINED VCS_TAG AND NOT "${VCS_TAG}" STREQUAL "")
# We're on an exact tag
# Try to extract version info from the tag
extractVersionNumberFromGitTag("${VCS_TAG}")

if(DID_EXTRACT_VERSION)
# Use the full tag for the EMSCRIPTEN_WZ_VERSIONSTRING
set(EMSCRIPTEN_WZ_VERSIONSTRING "${VCS_TAG}")
endif()
endif()

if(NOT DEFINED EMSCRIPTEN_WZ_VERSIONSTRING)
# Set the EMSCRIPTEN_WZ_VERSIONSTRING to: <VCS_BRANCH>@<VCS_SHORTHASH>
set(EMSCRIPTEN_WZ_VERSIONSTRING "${VCS_BRANCH}@${VCS_SHORT_HASH}")
endif()

##################################
# EMSCRIPTEN_WZ_GITCOMMIT

set(EMSCRIPTEN_WZ_GITCOMMIT "${VCS_FULL_HASH}")

##################################
# Debug output

execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++EMSCRIPTEN_WZ_VERSIONSTRING: ${EMSCRIPTEN_WZ_VERSIONSTRING}")
execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++EMSCRIPTEN_WZ_GITCOMMIT: ${EMSCRIPTEN_WZ_GITCOMMIT}")

##################################
# Output configured file based on the template

if(NOT EXISTS "${TEMPLATE_FILE}")
message( FATAL_ERROR "Input TEMPLATE_FILE does not exist: \"${TEMPLATE_FILE}\"" )
endif()
configure_file("${TEMPLATE_FILE}" "${OUTPUT_FILE}" @ONLY)

4 changes: 4 additions & 0 deletions platforms/emscripten/extern-postjs.js.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let WZ2100_WASM_CURRENT_BUILD_INFO = {
versionString: "@EMSCRIPTEN_WZ_VERSIONSTRING@",
gitCommit: "@EMSCRIPTEN_WZ_GITCOMMIT@"
};
24 changes: 23 additions & 1 deletion platforms/emscripten/shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ <h2 class="accordion-header">
</div>
</div>
<div class="modal-footer">
<div id="options_buildinfo" class="me-auto text-break small text-secondary lh-sm opacity-75" style="max-width: 400px">
<span class="small">Version: <span id="options_buildinfo_version">n/a</span></span>
<br>
<span class="small">Git Commit: <a id="options_buildinfo_commit" href="https://github.com/Warzone2100/warzone2100/commits/master" class="link-secondary" target="_blank" rel="noopener">n/a</a></span>
</div>
<button type="button" class="btn btn-primary" onclick="wz_dismiss_options_modal()">OK</button>
</div>
</div>
Expand Down Expand Up @@ -1507,10 +1512,27 @@ <h1 class="modal-title fs-5" id="refreshForUpdateLabel">Update Required</h1>
// ignore
});
}


function wz_set_options_build_info() {
if (typeof WZ2100_WASM_CURRENT_BUILD_INFO !== 'undefined') {
try {
let version_span = document.getElementById('options_buildinfo_version');
version_span.textContent = WZ2100_WASM_CURRENT_BUILD_INFO['versionString'];
let commit_link = document.getElementById('options_buildinfo_commit');
commit_link.href = "https://github.com/Warzone2100/warzone2100/commit/"+WZ2100_WASM_CURRENT_BUILD_INFO['gitCommit'];
commit_link.textContent = WZ2100_WASM_CURRENT_BUILD_INFO['gitCommit'].substring(0, 7);
} catch (err) {
console.error('Getting build info failed:', err);
}
} else {
console.error('Build info not available?');
}
}

// Initial page startup (once everything is loaded)
function wz_everything_is_loaded() {
wz_restore_launch_options();
wz_set_options_build_info();
checkWZBrowserSupport().then((r) => {
// All required features are available
wz_display_launch_game_msg();
Expand Down
24 changes: 24 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ if(CMAKE_SYSTEM_NAME MATCHES "Darwin")

endif()

# On Emscripten, generate the extern-postjs.js file with updated version info at *build-time*
if(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
set(_extern_postjs_template_file "${PROJECT_SOURCE_DIR}/platforms/emscripten/extern-postjs.js.in")
set(_extern_postjs_output_file "${CMAKE_CURRENT_BINARY_DIR}/extern-postjs.js")
add_custom_command(
OUTPUT "${_extern_postjs_output_file}"
COMMAND ${CMAKE_COMMAND} -DCACHEFILE=${wz2100_autorevision_cache_file} -DPROJECT_ROOT=${PROJECT_SOURCE_DIR} -DTEMPLATE_FILE=${_extern_postjs_template_file} -DOUTPUT_FILE=${_extern_postjs_output_file} -P ${CMAKE_SOURCE_DIR}/platforms/emscripten/cmake/autorevision_externpostjs.cmake
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
DEPENDS "${_extern_postjs_template_file}" "${wz2100_autorevision_cache_file}" "${CMAKE_SOURCE_DIR}/platforms/emscripten/cmake/autorevision_externpostjs.cmake"
VERBATIM
)

add_custom_target(autorevision_externpostjs ALL
DEPENDS
"${_extern_postjs_output_file}"
)
set_property(TARGET autorevision_externpostjs PROPERTY FOLDER "_WZBuildProcessTargets")
add_dependencies(autorevision_externpostjs autorevision) # Ensure ordering and non-concurrency
endif()

############################
# Main Executable

Expand Down Expand Up @@ -280,6 +300,10 @@ if(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
target_link_options(warzone2100 PRIVATE "SHELL:--pre-js ${PROJECT_SOURCE_DIR}/platforms/emscripten/prejs.js")
target_link_options(warzone2100 PRIVATE "SHELL:--post-js ${PROJECT_SOURCE_DIR}/platforms/emscripten/postjs.js")

# Autorevision extern-postjs
add_dependencies(warzone2100 autorevision autorevision_externpostjs)
target_link_options(warzone2100 PRIVATE "SHELL:--extern-post-js ${CMAKE_CURRENT_BINARY_DIR}/extern-postjs.js")

target_link_options(warzone2100 PRIVATE "SHELL:-s MODULARIZE")
target_link_options(warzone2100 PRIVATE "SHELL:-s EXPORT_NAME=createWZModule")
target_link_options(warzone2100 PRIVATE "SHELL:-s EXIT_RUNTIME")
Expand Down

0 comments on commit 54aebb1

Please sign in to comment.