Skip to content

build: CMake project fixes for MS Visual C++ compiler#11397

Open
mabrarov wants to merge 11 commits intofluent:masterfrom
mabrarov:feature/cmake_msvc
Open

build: CMake project fixes for MS Visual C++ compiler#11397
mabrarov wants to merge 11 commits intofluent:masterfrom
mabrarov:feature/cmake_msvc

Conversation

@mabrarov
Copy link
Contributor

@mabrarov mabrarov commented Jan 26, 2026

Summary

  • Fixed C/C++ compiler options in CMake project especially when MSVC is used (e.g. fixed passing preprocessor definitions required for Windows containers failing to build #10158). It makes possible debugging of (Windows build of) Fluent Bit / tests on Windows.
  • Fixed (formatting, build command) development guide for Windows.
  • Fixed support of "Visual Studio" CMake Generators - refer to Windows containers failing to build #10158.
    Note: multi-config CMake Generators (like "Visual Studio" ones) can still have issues due to Fluent Bit CMake project doesn't honor and doesn't support the build type(s) specified when CMake generates native build system project - e.g. CMAKE_BUILD_TYPE passed through command line and CMAKE_CONFIGURATION_TYPES. Support of multi-config CMake Generators can require usage of CMake generator expressions everywhere build-config specific compiler / linker options are added / modified, can require revisiting of the way linked libraries are specified, e.g. they should not use -l option with hard-coded name of static library file, because the name of library file can depend on build configuration on Windows - e.g. libcrypto.lib for Release and RelWithDebInfo builds with /MT compiler option (static release MS C/C++ runtime) vs libcryptod.lib for Debug build with /MTd compiler option (static debug MS C/C++ runtime).
  • Fixed parallel build of Fluent Bit when building docker image for Windows containers.
  • Fixed "negative coverage" error when collecting test coverage with coverage profiled build.
  • Speedup of building debug version on Windows by disabling Link-Time Code Generation for Debug build only.

Testing

Before we can approve your change; please submit the following in a comment:

  • [N/A] Example configuration file for the change.
  • [N/A] Debug log output from testing the change.
  • Attached Valgrind output that shows no leaks or memory corruption was found - refer to flb_run_code_analysis.log for the output of command
    TEST_PRESET=valgrind SKIP_TESTS='flb-rt-out_td flb-it-network' ./run_code_analysis.sh
  • [N/A] Run local packaging test showing all targets (including any new ones) build.
  • [N/A] Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • [N/A] Documentation required for this feature.

Backporting

  • [N/A] Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

  • Build & Infrastructure

    • Windows builder switched to Ninja; build invocation updated.
    • Added delayed-extension variables for C++ compiler/linker flags and applied when C++ is enabled.
    • Improved MSVC handling: UTF‑8 enabled; static-runtime selection moved to per-configuration logic; MK_DEBUG disabled on MSVC.
    • Coverage and platform-specific linker-flag handling improved; MPack tracking explicitly disabled.
  • Documentation

    • Windows setup docs updated with vcpkg integration and revised build commands.
  • Bug Fixes

    • Fixed 64-bit file-mapping size handling on Windows.

@coderabbitai
Copy link

coderabbitai bot commented Jan 26, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds delayed-extension CXX/link flag variables, moves MSVC runtime and /utf-8 handling into per-config delayed workflow, updates Windows builder to use Ninja/vcpkg, refines CMake flag handling across platforms, and fixes 64-bit file-mapping on Windows.

Changes

Cohort / File(s) Summary
Top-level CMake orchestrator
CMakeLists.txt
Adds FLB_EXTRA_CMAKE_CXX_FLAGS and FLB_EXTRA_CMAKE_CXX_LINK_FLAGS (empty defaults), implements delayed-extension merging for C++ flags, moves MSVC /MT management to per-config workflow, adds /utf-8 and coverage adjustments, and gates MK_DEBUG off for MSVC.
Onigmo MSVC CMake tweaks
lib/onigmo/CMakeLists.txt
Switches MSVC runtime handling to CMAKE_MSVC_RUNTIME_LIBRARY and per-config CMAKE_C_FLAGS_<CONFIG> transforms, adds /utf-8, removes global /MT injection and forced CMAKE_BUILD_TYPE, and conditions LTCG per-config.
Developer guide & vcpkg flow
DEVELOPER_GUIDE.md
Adds vcpkg manifest guidance, Windows vcpkg integration steps, refactors PowerShell Flex/Bison blocks, and updates final build invocation to --config Release --clean-first.
Windows Docker builder
dockerfiles/Dockerfile.windows
Installs Ninja, switches builder stage to Ninja generator, removes explicit -DCMAKE_BUILD_TYPE=Release and multi-config NMake invocation, and uses Ninja single-config build.
Platform/Coverage/MPack adjustments
CMakeLists.txt (Unicode/wasm/armv7l/FreeBSD/coverage sections)
Marks platform blocks to extend CXX/CXX_LINK flags via delayed-extension when C++ is enabled, adds -fprofile-update=atomic where applicable, and defines MPACK_READ_TRACKING/MPACK_WRITE_TRACKING to disable tracking.
Windows mapping fix
lib/chunkio/src/cio_file_win32.c
Add limits.h include and compute high/low 32-bit parts of mapping size safely; pass correct DWORD high/low to CreateFileMappingA for large mappings.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer/CI
    participant Docker as Windows Builder
    participant Ninja as Ninja
    participant CMake as CMake
    participant MSVC as MSVC Toolchain
    participant Linker as Linker

    Dev->>Docker: trigger Windows build
    Docker->>Ninja: install & use Ninja generator
    Ninja->>CMake: configure (reads FLB_EXTRA_CMAKE_CXX_* and vcpkg toolchain)
    CMake->>MSVC: apply per-config flags (/utf-8, runtime selection, delayed CXX flags)
    MSVC->>Linker: compile & link
    Linker->>Dev: produce artifact
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested reviewers

  • edsiper
  • cosmo0920
  • niedbalski
  • patrick-stephens
  • fujimotos
  • celalettin1286

Poem

🐰 I hid a flag where CMake sleeps,

Delayed till C++ wakes from heaps.
Ninja hopped in with vcpkg cheer,
UTF‑8 hummed, runtimes found their gear,
Mappings count true bytes — hop, hooray!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: CMake project fixes for the MS Visual C++ compiler, which aligns with the primary focus of the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mabrarov mabrarov marked this pull request as ready for review January 26, 2026 20:05
@mabrarov
Copy link
Contributor Author

This PR is ready for review. The failed CI jobs seem to be failing because of internal infrastructure issues now (previously it was this PR issue - refer to #11397 (comment)).

@mabrarov
Copy link
Contributor Author

mabrarov commented Feb 5, 2026

Hi @edsiper, @patrick-stephens and @cosmo0920 ,

I appreciate if you can find time to review this PR, because it is essential for development of Fluent Bit on / for Windows (e.g. debugging).

Thank you.

@patrick-stephens
Copy link
Contributor

Thanks for this @mabrarov, appreciate the effort you have made to provide this as its a hard target for us to support.

Can you check the CI is all green? Generally that's a blocker before we can review.

@mabrarov
Copy link
Contributor Author

mabrarov commented Feb 7, 2026

Hi @patrick-stephens,

Regarding your question about failed CI:

  1. It looks like run-windows-unit-tests / call-build-windows-unit-test (Windows 32bit, x86, x86-windows-static, 3.31.6, windows-latest) job fails consistently for this PR.
  2. Due to CI is unstable (tests are unstable on my local environment even for the master branch) - es_out: support Upstream Servers with configuration overriding #7608 is example of that - I was thinking it is caused by internal CI issue or by existing flaky test.
  3. Yesterday I setup local environment for x86 build with MSVC and found that the issue with x86 build (with tests) is stably reproduced for this PR and is not reproduced for the master branch. I feel I found the root cause - shift operator UB in case of 32-bit build in
    (DWORD)(actual_map_size >> 32),
    which I fixed in 4616e0b.

Thank you.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@CMakeLists.txt`:
- Around line 57-62: The current add_compile_definitions call unconditionally
defines _DEBUG for all compilers in Debug builds which affects bundled libraries
(e.g., lib/miniz's mz_zip_array_range_check) — change this to only add _DEBUG
for MSVC Debug builds by replacing the unconditional generator expression with a
conditional one that checks both Debug config and the MSVC compiler ID
(referencing add_compile_definitions and the _DEBUG symbol), preserving the
existing non-cache variables FLB_EXTRA_CMAKE_CXX_FLAGS and
FLB_EXTRA_CMAKE_CXX_LINK_FLAGS behavior.
🧹 Nitpick comments (1)
lib/onigmo/CMakeLists.txt (1)

33-49: MSVC runtime and UTF-8 handling mirrors the root CMakeLists.txt pattern.

The duplication of MSVC runtime handling here is necessary because onigmo's CMakeLists.txt resets CMAKE_C_FLAGS (line 61), and the per-config CMAKE_C_FLAGS_* variables need the MD→MT fixup in this scope as well.

Consider extracting the shared MSVC setup (runtime selection, UTF-8, per-config fixup) into a reusable CMake include file to reduce duplication across the root CMakeLists.txt and lib/onigmo/CMakeLists.txt.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@CMakeLists.txt`:
- Around line 65-82: The CMAKE_MSVC_RUNTIME_LIBRARY assignment is ignored
because policy CMP0091 defaults to OLD under the current cmake_minimum_required;
set the policy to NEW before you assign CMAKE_MSVC_RUNTIME_LIBRARY (use
cmake_policy(SET CMP0091 NEW) placed earlier than the
set(CMAKE_MSVC_RUNTIME_LIBRARY ...) line) so the variable takes effect; keep the
existing add_compile_options(/MT...) and the regex replacement loop
(string(REGEX REPLACE ...)) if you want to remain robust against toolchain files
that may still inject /MD.
🧹 Nitpick comments (1)
CMakeLists.txt (1)

712-727: Delayed CXX flag application after enable_language(CXX) — good pattern.

The /MD/MT replacement for CXX flags correctly mirrors the C flags handling above (lines 72–76), and applying FLB_EXTRA_CMAKE_CXX_FLAGS / FLB_EXTRA_CMAKE_CXX_LINK_FLAGS here ensures they only take effect once the CXX language is actually enabled.

Minor note: the concatenation on line 725 ("${CMAKE_CXX_FLAGS}${FLB_EXTRA_CMAKE_CXX_FLAGS}") relies on FLB_EXTRA_CMAKE_CXX_FLAGS having a leading space from how values are appended (e.g., line 485: "${FLB_EXTRA_CMAKE_CXX_FLAGS} -g -O0 ..."). This works but is a subtle convention. A space before ${FLB_EXTRA_CMAKE_CXX_FLAGS} would be more defensive.

Optional: explicit space separator
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${FLB_EXTRA_CMAKE_CXX_FLAGS}")
-  set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}${FLB_EXTRA_CMAKE_CXX_LINK_FLAGS}")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLB_EXTRA_CMAKE_CXX_FLAGS}")
+  set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} ${FLB_EXTRA_CMAKE_CXX_LINK_FLAGS}")

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
DEVELOPER_GUIDE.md (1)

800-857: ⚠️ Potential issue | 🟡 Minor

Useful addition of vcpkg manifest and build instructions for Windows Server 2022.

The step-by-step guide with vcpkg.json, dependency installation, and Visual Studio solution generation is clear and actionable.

One thing to note: the CMake configure command on line 845 uses the Visual Studio multi-config generator but doesn't set -DFLB_DEBUG=Off -DFLB_RELEASE=On, so FLB_DEBUG defaults to Yes. This means CMake-level debug definitions will be active even when building with --config Release on line 856. This is the multi-config generator limitation acknowledged in the PR objectives, so not a blocker — but it may be worth adding a brief note here to help developers avoid confusion.

🧹 Nitpick comments (1)
CMakeLists.txt (1)

712-726: Delayed CXX flag application is well-structured.

The pattern of accumulating flags in FLB_EXTRA_CMAKE_CXX_FLAGS / FLB_EXTRA_CMAKE_CXX_LINK_FLAGS before enable_language(CXX) and applying them after is correct — CMAKE_CXX_FLAGS isn't meaningful until the CXX language is enabled. The MSVC /MD/MT regex replacement for CMAKE_CXX_FLAGS_* variants mirrors the earlier C flags treatment (lines 72-76), maintaining consistency.

One minor note: the concatenation on line 725 ("${CMAKE_CXX_FLAGS}${FLB_EXTRA_CMAKE_CXX_FLAGS}") relies on accumulated values in FLB_EXTRA_CMAKE_CXX_FLAGS always having a leading space (e.g., line 485: " -g -O0 ..."). This works correctly given the current usage patterns, but is a fragile convention. Consider a space separator for robustness:

♻️ Optional: safer concatenation
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${FLB_EXTRA_CMAKE_CXX_FLAGS}")
-  set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}${FLB_EXTRA_CMAKE_CXX_LINK_FLAGS}")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLB_EXTRA_CMAKE_CXX_FLAGS}")
+  set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} ${FLB_EXTRA_CMAKE_CXX_LINK_FLAGS}")

@mabrarov
Copy link
Contributor Author

mabrarov commented Feb 16, 2026

FYI, the only remaining library linking issue with multi-config CMake generators and MSVC is LibYAML located at:

and

if(FLB_CONFIG_YAML)

There is a chance that it is the only remaining issue with multi-config CMake generators and MSVC at all.

Due to LibYAML (CMake project of LibYAML) doesn't provide its layout (location and naming of built libraries) for multi-config builds - release and debug versions of LibYAML library file have same file name and are placed in the same directory - a custom FindLibYAML CMake module (e.g. to use yaml.lib for release builds and yamld.lib for debug builds with MSVC) is debatable and is not included into this PR. I plan to add it in a separate PR (refer to feature/cmake_libyaml branch) once this PR is approved and merged.

Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
Copy link
Contributor

@cosmo0920 cosmo0920 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to handle patches for each of upstream repos under lib managed libraries.

Comment on lines 21 to +148
@@ -76,6 +77,8 @@ int cio_file_native_map(struct cio_file *cf, size_t map_size)
DWORD desired_access;
size_t file_size;
size_t actual_map_size;
DWORD actual_map_size_high;
DWORD actual_map_size_low;
int ret;

if (cf == NULL) {
@@ -131,10 +134,18 @@ int cio_file_native_map(struct cio_file *cf, size_t map_size)

/* CreateFileMappingA requires size as two DWORDs (high and low) */
/* Use actual_map_size to ensure consistency */
#if SIZE_MAX > MAXDWORD
actual_map_size_high = (DWORD)((actual_map_size >> (sizeof(DWORD) * CHAR_BIT))
& 0xFFFFFFFFUL);
actual_map_size_low = (DWORD)(actual_map_size & 0xFFFFFFFFUL);
#else
actual_map_size_high = 0;
actual_map_size_low = (DWORD)actual_map_size;
#endif
cf->backing_mapping = CreateFileMappingA(cf->backing_file, NULL,
desired_protection,
(DWORD)(actual_map_size >> 32),
(DWORD)(actual_map_size & 0xFFFFFFFFUL),
actual_map_size_high,
actual_map_size_low,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you send your patch into fluent/cmetrics?
It would be better to handle there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean fluent/chunkio?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-required ok-package-test Run PR packaging tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants