Skip to content

Commit

Permalink
Add support for ICU library bundled with Windows (#49)
Browse files Browse the repository at this point in the history
This adds the cmake option variable `UPA_USE_WINDOWS_ICU` and a
preprocessor macro of the same name for this purpose:

1. For cmake CLI add the option `-DUPA_USE_WINDOWS_ICU=ON`

2. Or define the preprocessor macro `UPA_USE_WINDOWS_ICU = 1` if you
   are using amalgamated sources
  • Loading branch information
rmisev authored Jul 8, 2024
1 parent c9b7de4 commit 45cfe00
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,48 @@ jobs:
generator: "Visual Studio 16 2019"
cxx_standard: 20
cmake_options: ""
install_icu: true

- name: VS 2022 C++14
os: windows-2022
generator: "Visual Studio 17 2022"
cxx_standard: 14
cmake_options: ""
install_icu: true

- name: VS 2022 C++17 (Windows ICU)
os: windows-2022
generator: "Visual Studio 17 2022"
cxx_standard: 17
cmake_options: "-DUPA_USE_WINDOWS_ICU=ON"

- name: VS 2022 C++23
os: windows-2022
generator: "Visual Studio 17 2022"
cxx_standard: 23
cmake_options: ""
install_icu: true

- name: VS 2022 Clang C++20
os: windows-2022
generator: "Visual Studio 17 2022"
cxx_standard: 20
cmake_options: "-T ClangCL"
install_icu: true

steps:
- uses: actions/checkout@v4
- name: install ICU library
if: matrix.install_icu
run: install-icu.bat C:\LIB 75 1
shell: cmd
- name: get dependencies
run: init.bat
shell: cmd
- name: cmake
run: cmake -S . -B build -G "${{ matrix.generator }}" -A x64 -DCMAKE_CXX_STANDARD=${{ matrix.cxx_standard }} -DICU_ROOT=C:\LIB\ICU ${{ matrix.cmake_options }}
run: cmake -S . -B build -G "${{ matrix.generator }}" -A x64 -DCMAKE_CXX_STANDARD=${{ matrix.cxx_standard }} ${{ matrix.cmake_options }}
env:
ICU_ROOT: C:\LIB\ICU
- name: build
run: cmake --build build --config Release
- name: test
Expand Down
19 changes: 14 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ project(upa_url VERSION ${UPA_URL_VERSION} LANGUAGES CXX)
# ${upa_lib_name}-config.cmake
# It also must be used as the package name argument to find_package
set(upa_lib_name upa)
set(upa_lib_name_in upa-icu)
# Exported name for library target files; also used to create an alias
# target: upa::${upa_lib_export}
set(upa_lib_export url)
Expand Down Expand Up @@ -54,6 +55,7 @@ option(UPA_BUILD_TOOLS "Build tools." OFF)
option(UPA_INSTALL "Generate the install target." ON)
# library options
option(UPA_AMALGAMATED "Use amalgamated URL library source." OFF)
option(UPA_USE_WINDOWS_ICU "Use ICU library bundled with Windows 10 version 1903 or later." OFF)
# tests build options
option(UPA_TEST_COVERAGE "Build tests with code coverage reporting" OFF)
option(UPA_TEST_COVERAGE_CLANG "Build tests with Clang source-based code coverage" OFF)
Expand Down Expand Up @@ -115,8 +117,10 @@ include_directories(deps)
# Are Upa URL and ICU libraries needed?
if (UPA_BUILD_TESTS OR UPA_BUILD_BENCH OR UPA_BUILD_FUZZER OR UPA_BUILD_EXAMPLES OR
UPA_BUILD_EXTRACTED OR UPA_INSTALL OR NOT UPA_BUILD_TOOLS)
# This library depends on ICU
find_package(ICU REQUIRED COMPONENTS i18n uc)
if (NOT UPA_USE_WINDOWS_ICU)
# This library depends on ICU
find_package(ICU REQUIRED COMPONENTS i18n uc)
endif()

if (UPA_AMALGAMATED)
add_library(${upa_lib_target} STATIC
Expand All @@ -138,8 +142,13 @@ if (UPA_BUILD_TESTS OR UPA_BUILD_BENCH OR UPA_BUILD_FUZZER OR UPA_BUILD_EXAMPLES
add_library(upa::${upa_lib_export} ALIAS ${upa_lib_target})
set_target_properties(${upa_lib_target} PROPERTIES
EXPORT_NAME ${upa_lib_export})
target_include_directories(${upa_lib_target} PRIVATE ${ICU_INCLUDE_DIR})
target_link_libraries(${upa_lib_target} INTERFACE ICU::i18n ICU::uc)
if (UPA_USE_WINDOWS_ICU)
target_compile_definitions(${upa_lib_target} PRIVATE UPA_USE_WINDOWS_ICU=1)
set(upa_lib_name_in ${upa_lib_name})
else()
target_include_directories(${upa_lib_target} PRIVATE ${ICU_INCLUDE_DIR})
target_link_libraries(${upa_lib_target} INTERFACE ICU::i18n ICU::uc)
endif()
endif()

# Test targets
Expand Down Expand Up @@ -264,7 +273,7 @@ if (UPA_INSTALL AND NOT UPA_AMALGAMATED)

# generate the config file that includes the exports
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${upa_lib_name}-config.cmake.in
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${upa_lib_name_in}-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${upa_lib_name}-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${upa_lib_name}
NO_SET_AND_CHECK_MACRO
Expand Down
3 changes: 0 additions & 3 deletions cmake/upa-config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(ICU REQUIRED COMPONENTS i18n uc)

include("${CMAKE_CURRENT_LIST_DIR}/upa-targets.cmake")
6 changes: 6 additions & 0 deletions cmake/upa-icu-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(ICU REQUIRED COMPONENTS i18n uc)

include("${CMAKE_CURRENT_LIST_DIR}/upa-targets.cmake")
20 changes: 16 additions & 4 deletions src/url_idna.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
//

// Define UPA_USE_WINDOWS_ICU = 1 to use the ICU library bundled with
// Windows 10 version 1903 or later. For more information, see:
// https://learn.microsoft.com/en-us/windows/win32/intl/international-components-for-unicode--icu-
#ifndef UPA_USE_WINDOWS_ICU
# define UPA_USE_WINDOWS_ICU 0 // NOLINT(*-macro-*)
#endif // UPA_USE_WINDOWS_ICU

#include "upa/config.h"
#include "upa/url_idna.h"
#include "upa/util.h"

#if UPA_USE_WINDOWS_ICU
# include <icu.h>
# pragma comment( lib, "icu" )
#else
// ICU: only C API is used (U_SHOW_CPLUSPLUS_API 0)
// https://unicode-org.github.io/icu/userguide/icu4c/build.html#icu-as-a-system-level-library
#define U_SHOW_CPLUSPLUS_API 0 // NOLINT(*-macro-*)
#include "unicode/uchar.h" // u_getUnicodeVersion
#include "unicode/uclean.h"
#include "unicode/uidna.h"
# define U_SHOW_CPLUSPLUS_API 0 // NOLINT(*-macro-*)
# include "unicode/uchar.h" // u_getUnicodeVersion
# include "unicode/uclean.h"
# include "unicode/uidna.h"
#endif

#include <algorithm>
#include <cassert>
Expand Down

0 comments on commit 45cfe00

Please sign in to comment.