Skip to content

Commit

Permalink
Merge pull request #84 from NuiCpp/devel
Browse files Browse the repository at this point in the history
Inline Code Fixes
  • Loading branch information
5cript authored Oct 21, 2023
2 parents fd957b0 + e9bfa9e commit 8eb1237
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 28 deletions.
1 change: 1 addition & 0 deletions cmake/backend/emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function(nui_add_emscripten_target)
${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_CMAKE_OPTIONS}
-DNUI_INLINE_EXTRACTOR_TARGET_FILE=$<TARGET_FILE:inline-parser>
-DNUI_INLINE_INJECTOR_TARGET_FILE=$<TARGET_FILE:inline-injector>
-DNUI_MODULE_BUILD_DIR=${CMAKE_BINARY_DIR}/module_${NUI_ADD_EMSCRIPTEN_TARGET_ARGS_TARGET}
"${SOURCE_DIR}"
# copy over package.json and fill parcel options that do not exist on it
${BUILD_COMMAND}
Expand Down
4 changes: 2 additions & 2 deletions cmake/frontend/emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function(nui_prepare_emscripten_target)

set(INLINER_COMMAND "")
if (NOT NO_INLINE)
nui_enable_inline(TARGET ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_TARGET})
nui_enable_inline(TARGET ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_TARGET} RELATIVE_TO ${CMAKE_CURRENT_SOURCE_DIR})
if (NOT NO_INLINE_INJECT)
set(INLINER_COMMAND COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/static/index.html" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.js" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.css")
endif()
Expand All @@ -34,7 +34,7 @@ function(nui_prepare_emscripten_target)

add_custom_target(
${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_TARGET}-parcel
COMMAND ${CMAKE_COMMAND} -E copy_directory "${NUI_SOURCE_DIRECTORY}/nui/js" "${CMAKE_BINARY_DIR}/nui-js"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${NUI_SOURCE_DIRECTORY}/nui/js" "${NUI_MODULE_BUILD_DIR}/nui-js"
COMMAND ${CMAKE_COMMAND} -E copy_directory ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_STATIC} "${CMAKE_BINARY_DIR}/static"
${INLINER_COMMAND}
COMMAND "${CMAKE_BINARY_DIR}/node_modules/.bin/parcel" build --dist-dir "${CMAKE_BINARY_DIR}/bin" ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_PARCEL_ARGS}
Expand Down
53 changes: 34 additions & 19 deletions cmake/inline_extractor.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,80 @@ function(nui_preprocess_inline_js)
set(multi_value_args EXTRA_CXX_FLAGS)
cmake_parse_arguments(CPP "" "${one_value_args}" "${multi_value_args}" ${ARGN})

string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
string(REPLACE " " ";" c_flags "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${build_type}}")
set(CURRENT_BUILD_TYPE "Release")
if (CMAKE_BUILD_TYPE)
set(CURRENT_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
endif()

string(TOUPPER ${CURRENT_BUILD_TYPE} PREPROCESS_BUILD_TYPE)
string(REPLACE " " ";" PREPROCESS_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${PREPROCESS_BUILD_TYPE}}")

set(INCLUDE_DIRS "$<TARGET_PROPERTY:${CPP_TARGET},INCLUDE_DIRECTORIES>")
set(COMPILE_DEFINITIONS "$<TARGET_PROPERTY:${CPP_TARGET},COMPILE_DEFINITIONS>")
add_custom_command(
OUTPUT ${CPP_OUTPUT}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CPP_DIRECTORY}
COMMAND ${CMAKE_CXX_COMPILER}
#"-D$<JOIN:$<TARGET_PROPERTY:${CPP_TARGET},COMPILE_DEFINITIONS>,;-D>"
"-I$<JOIN:$<TARGET_PROPERTY:${CPP_TARGET},INCLUDE_DIRECTORIES>,;-I>"
${c_flags}
"$<$<BOOL:${COMPILE_DEFINITIONS}>:-D$<JOIN:${COMPILE_DEFINITIONS},;-D>>"
"$<$<BOOL:${INCLUDE_DIRS}>:-I$<JOIN:${INCLUDE_DIRS},;-I>>"
${PREPROCESS_CXX_FLAGS}
$<TARGET_PROPERTY:${CPP_TARGET},COMPILE_OPTIONS>
${CPP_EXTRA_CXX_FLAGS}
-E ${CPP_SOURCE} -o ${CPP_OUTPUT}
# COMMAND "${NUI_INLINE_EXTRACTOR_TARGET_FILE}" ${CPP_INLINE_CACHE} ${CPP_OUTPUT} ${CPP_IS_FIRST}
COMMAND_EXPAND_LISTS VERBATIM
IMPLICIT_DEPENDS C ${CPP_SOURCE}
DEPENDS ${CPP_SOURCE} ${CPP_DEPENDS})
endfunction()

function(nui_enable_inline)
set(one_value_args TARGET UNPACKED_MODE)
set(one_value_args TARGET UNPACKED_MODE RELATIVE_TO)
set(multi_value_args)
cmake_parse_arguments(nui_enable_inline_ARGS "" "${one_value_args}" "${multi_value_args}" ${ARGN})

get_target_property(INLINE_JS_SOURCES ${nui_enable_inline_ARGS_TARGET} SOURCES)
get_target_property(INLINE_SOURCES ${nui_enable_inline_ARGS_TARGET} SOURCES)
message(STATUS "Inline sources for ${nui_enable_inline_ARGS_TARGET}")

set(INLINE_DIRECTORY_SUBDIR "nui-inline")
set(INLINE_DIRECTORY "${CMAKE_BINARY_DIR}/${INLINE_DIRECTORY_SUBDIR}")
set(INLINE_DIRECTORY "${NUI_MODULE_BUILD_DIR}/${INLINE_DIRECTORY_SUBDIR}")
set(INLINE_CACHE "${INLINE_DIRECTORY}/inline.cache")
set(INLINE_IMPORTS_SCRIPTS "${INLINE_DIRECTORY}/inline_imports.js")
set(INLINE_IMPORTS_STYLES "${INLINE_DIRECTORY}/inline_imports.css")

# for each source file preprocess it:
set(IS_FIRST TRUE)
foreach(source_file ${INLINE_JS_SOURCES})
get_filename_component(source_file_name "${source_file}" NAME)
set(preprocessed_source_file "${INLINE_DIRECTORY}/${source_file_name}.i")
foreach(SOURCE_FILE ${INLINE_SOURCES})
# Get relative path to source file
file(RELATIVE_PATH SOURCE_FILE_NAME_RELATIVE "${nui_enable_inline_ARGS_RELATIVE_TO}" "${SOURCE_FILE}")
get_filename_component(SOURCE_FILE_NAME "${SOURCE_FILE_NAME_RELATIVE}" NAME_WE)
# Get just the directory part of the source file
get_filename_component(SOURCE_FILE_DIR "${SOURCE_FILE_NAME_RELATIVE}" DIRECTORY)
if (NOT "${SOURCE_FILE_DIR}" STREQUAL "")
set(PREPROCESSED_SOURCE_FILE "${INLINE_DIRECTORY}/${SOURCE_FILE_DIR}/${SOURCE_FILE_NAME}.i")
else()
set(PREPROCESSED_SOURCE_FILE "${INLINE_DIRECTORY}/${SOURCE_FILE_NAME}.i")
endif()
nui_preprocess_inline_js(
TARGET ${nui_enable_inline_ARGS_TARGET}
DIRECTORY "${INLINE_DIRECTORY}"
DIRECTORY "${INLINE_DIRECTORY}/${SOURCE_FILE_DIR}"
INLINE_CACHE "${INLINE_CACHE}"
SOURCE "${source_file}"
OUTPUT "${preprocessed_source_file}"
SOURCE "${SOURCE_FILE}"
OUTPUT "${PREPROCESSED_SOURCE_FILE}"
EXTRA_CXX_FLAGS -P -CC -DNUI_INLINE -DNUI_MODULE_SOURCE_DIR="${CMAKE_SOURCE_DIR}" -DNUI_MODULE_CURRENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
IS_FIRST ${IS_FIRST}
)
set(IS_FIRST FALSE)
message(STATUS "Preprocessing ${source_file} to ${preprocessed_source_file}")
list(APPEND preprocessed_sources "${preprocessed_source_file}")
message(STATUS "Preprocessing ${SOURCE_FILE} to ${PREPROCESSED_SOURCE_FILE}")
list(APPEND PREPROCESSED_SOURCES "${PREPROCESSED_SOURCE_FILE}")
endforeach()

add_custom_command(
OUTPUT
${INLINE_IMPORTS_SCRIPTS}
${INLINE_IMPORTS_STYLES}
COMMAND ${CMAKE_COMMAND} -E make_directory ${INLINE_DIRECTORY}
COMMAND "${NUI_INLINE_EXTRACTOR_TARGET_FILE}" ${INLINE_CACHE} ${CMAKE_BINARY_DIR} ${INLINE_DIRECTORY_SUBDIR} ${preprocessed_sources}
COMMAND "${NUI_INLINE_EXTRACTOR_TARGET_FILE}" ${INLINE_CACHE} ${CMAKE_BINARY_DIR} ${INLINE_DIRECTORY_SUBDIR} ${PREPROCESSED_SOURCES}
COMMAND_EXPAND_LISTS VERBATIM
DEPENDS ${preprocessed_sources}
DEPENDS ${PREPROCESSED_SOURCES}
)

add_custom_target(
Expand Down
6 changes: 3 additions & 3 deletions examples/basic/backend/source/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(basic-example PRIVATE main.cpp)
target_sources(basic-example PRIVATE main.cpp)

target_include_directories(basic-example PRIVATE ${CMAKE_CURRENT_LIST_DIR}/../../include)

Expand All @@ -11,10 +11,10 @@ set_target_properties(basic-example
)

# Creates a target that is compiled through emscripten. This target becomes the frontend part.
nui_add_emscripten_target(basic-example
nui_add_emscripten_target(
TARGET
basic-example
PREJS
PREJS
${PREJS_FILE}
SOURCE_DIR
${CMAKE_SOURCE_DIR}
Expand Down
7 changes: 7 additions & 0 deletions nui/include/nui/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ namespace Nui
*/
void* getNativeWebView();

/**
* @brief Get the Native Window object
*
* @return void*
*/
void* getNativeWindow();

/**
* @brief [LINUX ONLY] Enable/Disable console output from view in the console.
*/
Expand Down
7 changes: 6 additions & 1 deletion nui/src/nui/backend/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ namespace Nui
name: name,
id: id,
args: [...args]
}}))
}}))
}};
}})();
)",
Expand Down Expand Up @@ -518,6 +518,11 @@ namespace Nui
return static_cast<webview::browser_engine&>(impl_->view).webview();
}
//---------------------------------------------------------------------------------------------------------------------
void* Window::getNativeWindow()
{
return impl_->view.window();
}
//---------------------------------------------------------------------------------------------------------------------
void Window::openDevTools()
{
std::scoped_lock lock{impl_->viewGuard};
Expand Down
6 changes: 3 additions & 3 deletions tools/inline_injector/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ int main(int argc, char** argv)
std::filesystem::relative(index.parent_path() / ".." / "bin" / "index.js", index.parent_path());

const std::string importScriptsHtml = "\t<script type=\"module\" defer>\n\t\timport \"" +
relativeImportScriptsFile.generic_string() + "\";\n</script>\n";
relativeImportScriptsFile.generic_string() + "\";\n\t</script>\n";
const std::string importStylesHtml =
"\t<style>\n\t\t@import \"" + relativeImportStylesFile.generic_string() + "\";\n</style>\n";
"\t<style>\n\t\t@import \"" + relativeImportStylesFile.generic_string() + "\";\n\t</style>\n";
const std::string importBinIndexHtml =
"\t<script type=\"module\" defer>\n\t\timport \"" + binIndex.generic_string() + "\";\n</script>\n";
"\t<script type=\"module\" defer>\n\t\timport \"" + binIndex.generic_string() + "\";\n\t</script>\n";

// find end of header </head> from behind in indexHtml:
const auto headEnd = indexHtml.rfind("</head>");
Expand Down

0 comments on commit 8eb1237

Please sign in to comment.