From 37d9a034013b4245a50c0d748dc83d2c3d136210 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Mon, 8 Sep 2025 23:08:10 +0800 Subject: Fix windows dynamic lib build. --- .github/workflows/ci.yml | 30 ++++++++++++++++++++++++------ CMakeLists.txt | 4 ++++ demos/ScrollView/main.cpp | 4 ++-- demos/main/main.cpp | 8 ++++---- include/cru/base/String.h | 13 +++++-------- include/cru/platform/GraphicsBase.h | 4 ++-- include/cru/platform/gui/UiApplication.h | 5 +---- include/cru/ui/controls/TextBlock.h | 13 ++++++++----- src/base/String.cpp | 14 ++++++++++++-- src/platform/gui/UiApplication.cpp | 7 ++++++- 10 files changed, 68 insertions(+), 34 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40982c43..d745ba51 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,9 +32,8 @@ jobs: cmake --build build --config Debug --target all - name: Test - working-directory: build run: | - ctest -C Debug -T test --output-on-failure + ctest --test-dir build -C Debug --output-on-failure windows-build: name: Windows Build @@ -53,9 +52,29 @@ jobs: cmake --build build --config Debug --target all - name: Test - working-directory: build run: | - ctest -C Debug -T test --output-on-failure + ctest --test-dir build -C Debug --output-on-failure + + windows-build-dynamic: + name: Windows Build (Dynamic Linking) + runs-on: windows-latest + + steps: + - uses: actions/checkout@v5 + with: + submodules: true + + - name: Build + run: | + . tools/Use-VC.ps1 + Use-VC + cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=1 -S. -Bbuild-dynamic -G Ninja + cmake --build build-dynamic --config Debug --target all + + - name: Test + run: | + ctest --test-dir build-dynamic -C Debug --output-on-failure + linux-build: name: Linux Build @@ -77,6 +96,5 @@ jobs: cmake --build build --config Debug --target all - name: Test - working-directory: build run: | - ctest -C Debug -T test --output-on-failure + ctest --test-dir build -C Debug --output-on-failure diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b26a29d..64b0b936 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,10 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output) + project(cru) if (APPLE) diff --git a/demos/ScrollView/main.cpp b/demos/ScrollView/main.cpp index b049a408..c7b4d0d6 100644 --- a/demos/ScrollView/main.cpp +++ b/demos/ScrollView/main.cpp @@ -18,7 +18,7 @@ int main() { ScrollView scroll_view; window.AddChild(&scroll_view); - TextBlock text_block( + auto text_block = TextBlock::Create( uR"([Verse 1] The snow glows white on the mountain tonight Not a footprint to be seen @@ -78,7 +78,7 @@ Let the storm rage on! The cold never bothered me anyway)", true); - scroll_view.SetChild(&text_block); + scroll_view.SetChild(text_block.get()); window.GetWindowHost()->GetNativeWindow()->SetVisibility( cru::platform::gui::WindowVisibilityType::Show); diff --git a/demos/main/main.cpp b/demos/main/main.cpp index d17f42ba..d574670d 100644 --- a/demos/main/main.cpp +++ b/demos/main/main.cpp @@ -45,12 +45,12 @@ int main() { window.AddChild(&flex_layout); - TextBlock text_block(u"Hello World from CruUI!", true); - flex_layout.AddChild(&text_block); + auto text_block = TextBlock::Create(u"Hello World from CruUI!", true); + flex_layout.AddChild(text_block.get()); - TextBlock button_text_block(u"OK"); + auto button_text_block = TextBlock::Create(u"OK"); Button button; - button.SetChild(&button_text_block); + button.SetChild(button_text_block.get()); flex_layout.AddChild(&button); TextBox text_box; diff --git a/include/cru/base/String.h b/include/cru/base/String.h index e58f21d3..7d462a6e 100644 --- a/include/cru/base/String.h +++ b/include/cru/base/String.h @@ -65,7 +65,7 @@ class CRU_BASE_API String { #endif public: - String() = default; + String(); String(const_pointer str); String(const_pointer str, size_type size); @@ -75,7 +75,7 @@ class CRU_BASE_API String { : String(reinterpret_cast(str), size - 1) {} template - String(Iter start, Iter end) { + String(Iter start, Iter end): String() { for (; start != end; start++) { append(*start); } @@ -250,12 +250,9 @@ class CRU_BASE_API String { } private: - static char16_t kEmptyBuffer[1]; - - private: - char16_t* buffer_ = kEmptyBuffer; - Index size_ = 0; // not including trailing '\0' - Index capacity_ = 0; // always 1 smaller than real buffer size + char16_t* buffer_; + Index size_; // not including trailing '\0' + Index capacity_; // always 1 smaller than real buffer size }; CRU_BASE_API std::ostream& operator<<(std::ostream& os, const String& value); diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h index c9f7626c..b0f653ef 100644 --- a/include/cru/platform/GraphicsBase.h +++ b/include/cru/platform/GraphicsBase.h @@ -53,8 +53,8 @@ inline String ToString(const Point& point) { } struct Size final { - static const Size kMax; - static const Size kZero; + static CRU_PLATFORM_API const Size kMax; + static CRU_PLATFORM_API const Size kZero; constexpr Size() = default; constexpr Size(const float width, const float height) diff --git a/include/cru/platform/gui/UiApplication.h b/include/cru/platform/gui/UiApplication.h index 3f36c26c..b44a450f 100644 --- a/include/cru/platform/gui/UiApplication.h +++ b/include/cru/platform/gui/UiApplication.h @@ -15,10 +15,7 @@ namespace cru::platform::gui { // The entry point of a ui application. struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { public: - static IUiApplication* GetInstance() { return instance; } - - private: - static IUiApplication* instance; + static IUiApplication* GetInstance(); protected: IUiApplication(); diff --git a/include/cru/ui/controls/TextBlock.h b/include/cru/ui/controls/TextBlock.h index ad44ad2d..52e227eb 100644 --- a/include/cru/ui/controls/TextBlock.h +++ b/include/cru/ui/controls/TextBlock.h @@ -16,6 +16,14 @@ class CRU_UI_API TextBlock : public NoChildControl, public: static constexpr StringView kControlType = u"TextBlock"; + static std::unique_ptr Create(String text, + bool selectable = false) { + auto c = std::make_unique(); + c->SetText(std::move(text)); + c->SetSelectable(selectable); + return std::move(c); + } + public: TextBlock(); TextBlock(const TextBlock& other) = delete; @@ -24,11 +32,6 @@ class CRU_UI_API TextBlock : public NoChildControl, TextBlock& operator=(TextBlock&& other) = delete; ~TextBlock() override; - TextBlock(String text, bool selectable = false) : TextBlock() { - SetText(std::move(text)); - SetSelectable(selectable); - } - String GetControlType() const final { return kControlType.ToString(); } render::RenderObject* GetRenderObject() const override; diff --git a/src/base/String.cpp b/src/base/String.cpp index e9623201..29cebde3 100644 --- a/src/base/String.cpp +++ b/src/base/String.cpp @@ -54,7 +54,13 @@ String String::FromStdPath(const std::filesystem::path& path) { return String::FromUtf8(path.string()); } -char16_t String::kEmptyBuffer[1] = {0}; +namespace { +char16_t kEmptyBuffer[1] = {0}; +} + +String::String() + : buffer_(kEmptyBuffer), + size_(0), capacity_(0) {} String::String(const_pointer str) : String(str, GetStrSize(str)) {} @@ -83,7 +89,11 @@ String::String(const wchar_t* str, Index size) #endif String::String(const String& other) { - if (other.size_ == 0) return; + if (other.size_ == 0) { + this->buffer_ = kEmptyBuffer; + this->size_ = this->capacity_ = 0; + return; + } this->buffer_ = new value_type[other.size_ + 1]; std::memcpy(this->buffer_, other.buffer_, other.size_ * sizeof(value_type)); this->buffer_[other.size_] = 0; diff --git a/src/platform/gui/UiApplication.cpp b/src/platform/gui/UiApplication.cpp index 4c6f5e1d..7bc27847 100644 --- a/src/platform/gui/UiApplication.cpp +++ b/src/platform/gui/UiApplication.cpp @@ -3,7 +3,12 @@ #include "cru/base/Exception.h" namespace cru::platform::gui { -IUiApplication* IUiApplication::instance = nullptr; + +namespace { +IUiApplication* instance = nullptr; +} + +IUiApplication* IUiApplication::GetInstance() { return instance; } IUiApplication::IUiApplication() { if (instance) { -- cgit v1.2.3