createLinuxBuildFile function

void createLinuxBuildFile(
  1. String dirPath, [
  2. bool? force
])

Creates the native_splash_screen.cmake file in dirPath. If force is true, it will overwrite the file if it exists.

Implementation

void createLinuxBuildFile(String dirPath, [bool? force]) {
  final file = File('$dirPath/native_splash_screen.cmake');
  if (file.existsSync() && force != true) {
    logger.w(
      'CMake file already exists at ${file.path}\n'
      'Use --force to overwrite.',
    );
    return;
  }

  const cmakeContent = r'''
# Generated file - do not edit
# Generated by native_splash_screen_cli

# Determine splash screen files for different build types
set(SPLASH_SCREEN_FILE_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/native_splash_screen_debug.cc")
set(SPLASH_SCREEN_FILE_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/native_splash_screen_release.cc")
set(SPLASH_SCREEN_FILE_PROFILE "${CMAKE_CURRENT_SOURCE_DIR}/native_splash_screen_profile.cc")

# Validate that all expected files exist
foreach(CONFIG_TYPE IN ITEMS DEBUG RELEASE PROFILE)
    string(TOLOWER "${CONFIG_TYPE}" CONFIG_TYPE_LOWER)
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/native_splash_screen_${CONFIG_TYPE_LOWER}.cc")
        message(FATAL_ERROR
            "\n-------------------------------------------------------------\n"
            "Missing splash screen configuration for ${CONFIG_TYPE} mode:\n"
            "${CMAKE_CURRENT_SOURCE_DIR}/native_splash_screen_${CONFIG_TYPE_LOWER}.cc\n\n"
            "Please run the following command in your project root:\n"
            "   dart run native_splash_screen_cli gen\n"
            "This will generate all necessary configuration files.\n"
            "-------------------------------------------------------------\n")
    endif()
endforeach()

# Create a static library for the splash screen code
add_library(native_splash_screen_linux STATIC "")

# Dynamically select the correct splash screen source file per config
target_sources(native_splash_screen_linux
  PRIVATE
    "$<IF:$<CONFIG:Debug>,${SPLASH_SCREEN_FILE_DEBUG},$<IF:$<CONFIG:Profile>,${SPLASH_SCREEN_FILE_PROFILE},${SPLASH_SCREEN_FILE_RELEASE}>>"
)

# Set properties for the library
set_target_properties(native_splash_screen_linux PROPERTIES
  CXX_STANDARD 17
  CXX_STANDARD_REQUIRED YES
  POSITION_INDEPENDENT_CODE ON
)

# Include directories
target_include_directories(native_splash_screen_linux PUBLIC
  "${CMAKE_CURRENT_SOURCE_DIR}"
)

# Warn if CMAKE_BUILD_TYPE is unset in single-config environments
if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
  message(WARNING "CMAKE_BUILD_TYPE not set. Defaulting to Release splash screen.")
endif()
''';

  file.writeAsStringSync(cmakeContent.trim());
  logger.success('CMake file created at ${file.path}');
}