Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUILD] Fixing CMake to build GTest on Windows #1887

Merged
merged 25 commits into from
May 26, 2023

Conversation

bachittle
Copy link
Contributor

Changes

Trying to compile with CMake on Windows. It breaks because of the Required clause. Removing it gets it working and installs required vcpkg files.

some improvements to consider:

  • this vcpkg run is very slow
  • googletest is already added in third_party (default command the docs tell you to run is --recursive, which gets googletest)
  • CHANGELOG.md updated for non-trivial changes (change is trivial)
  • Unit tests have been added (comment if this is needed)
  • Changes in public API reviewed (no changes to public API)

@bachittle bachittle requested a review from a team December 22, 2022 22:06
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 22, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Dec 22, 2022

Codecov Report

Merging #1887 (7349c5b) into main (5e290c9) will increase coverage by 0.03%.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1887      +/-   ##
==========================================
+ Coverage   87.15%   87.17%   +0.03%     
==========================================
  Files         166      166              
  Lines        4777     4777              
==========================================
+ Hits         4163     4164       +1     
+ Misses        614      613       -1     

see 1 file with indirect coverage changes

@bachittle
Copy link
Contributor Author

well it seems that even with this change the code still doesn't compile, it can't seem to find the vcpkg libraries afterwards. I might just experiment with including GTest manually like how it is done for linux (just including .so files instead of .a files)

@marcalff marcalff added the pr:waiting-on-cla Waiting on CLA label Jan 2, 2023
@ThomsonTan
Copy link
Contributor

well it seems that even with this change the code still doesn't compile, it can't seem to find the vcpkg libraries afterwards. I might just experiment with including GTest manually like how it is done for linux (just including .so files instead of .a files)

Wondering whether the vcpkg packages were installed correctly to <repo_root>\tools\vcpkg? The vcpkg is added to current CMake search path as below.

https://github.com/open-telemetry/opentelemetry-cpp/blob/main/CMakeLists.txt#L217

@bachittle
Copy link
Contributor Author

@ThomsonTan the vcpkg root was the issue, it would find it on first install but not on consecutive runs. Now I got my code to compile, but the test cases fail. Most likely due to GMOCK_LIB dependency. I couldn't find a vcpkg dependency of something that is similar to libgmock-dev that allowed me to get it to work on linux...

@lalitb
Copy link
Member

lalitb commented Jan 4, 2023

@ThomsonTan the vcpkg root was the issue, it would find it on first install but not on consecutive runs. Now I got my code to compile, but the test cases fail. Most likely due to GMOCK_LIB dependency. I couldn't find a vcpkg dependency of something that is similar to libgmock-dev that allowed me to get it to work on linux...

What is the failure error in tests ? gmock should be installed as part of gtest installation using vcpkg.

@bachittle
Copy link
Contributor Author

It works now. Only one failed test, which is also what showed up in my linux tests. Setting the toolchain file so late in the build process seems to not have an effect on finding proper packages, so I just set it manually under cmake-gui. Not ideal, but if there are any better solutions let me know!

@lalitb
Copy link
Member

lalitb commented Jan 30, 2023

@bachittle - Can you fix the CLA, so that it can be reviewed and merged?

@bachittle
Copy link
Contributor Author

It says that EasyCLA check passed, but now there are other CICD issues...

CMakeLists.txt Outdated
# set(CMAKE_TOOLCHAIN_FILE
# ${CMAKE_CURRENT_SOURCE_DIR}/tools/vcpkg/scripts/buildsystems/vcpkg.cmake
# PARENT_SCOPE)
message("Make sure that vcpkg.cmake is set as the CMAKE_TOOLCHAIN_FILE at the START of the cmake build process!
Copy link
Member

Choose a reason for hiding this comment

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

Can we print this message only when we do not set toolchain file?

if(NOT CMAKE_TOOLCHAIN_FILE)
  message(WARNING "Make sure that vcpkg.cmake is set as the CMAKE_TOOLCHAIN_FILE at the START of the cmake build process!
Can be command-line arg (cmake -DCMAKE_TOOLCHAIN_FILE=...) or set in your editor of choice.")
endif()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is the cmakefile tries to set the CMAKE_TOOLCHAIN_FILE in the cache at the start. This does not work unless its at the very start of the build process.

image

So its set, but it wasn't detecting my vcpkg unless I set it manually using cmake -DCMAKE_TOOLCHAIN_FILE=...
It may have to do with the submodules thinking the source directory is different, but gtest was not getting detected even after running install_windows_deps()

Copy link
Member

@owent owent Feb 3, 2023

Choose a reason for hiding this comment

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

If we use cmake -DCMAKE_TOOLCHAIN_FILE=... to set toolchain, we should also replace NOT DEFINED CMAKE_TOOLCHAIN_FILE by (NOT DEFINED CMAKE_TOOLCHAIN_FILE AND NOT DEFINED CACHE{CMAKE_TOOLCHAIN_FILE}), or maybe NOT CMAKE_TOOLCHAIN_FILE for short.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can also just be removed entirely since it doesn't work

Copy link
Member

@owent owent Feb 4, 2023

Choose a reason for hiding this comment

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

Sorry for my mistake, DEFINED <VAR NAME> can also be used to check cached variables.
LGTM after format problem is solved.Thanks for your contribution.

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm the ci format is still failing, any reason as to why?

Hi @bachittle, could you please also take a look at my comment here?

#1887 (comment)

Copy link
Contributor Author

@bachittle bachittle Feb 27, 2023

Choose a reason for hiding this comment

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

sorry I didn't see this, will take a look. The ci format isn't failing anymore, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
these are my cmake flags that are set using cmake-gui. notice the WITH_OTLP is not set, so I am just building the tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @bachittle. This does confirm that OTLP is not enabled. Instead of commenting out the set of CMAKE_TOOLCHAIN_FILE, can we add a line like below in the call after find_package(GTest REQUIRED) in the same file?

include(${CMAKE_TOOLCHAIN_FILE})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried this and it still failed, check latest commit: 2bd872b. Let me know if I should try putting this somewhere else.

@marcalff marcalff removed the pr:waiting-on-cla Waiting on CLA label Feb 6, 2023
@bachittle
Copy link
Contributor Author

hmm the ci format is still failing, any reason as to why?

@ThomsonTan
Copy link
Contributor

hmm the ci format is still failing, any reason as to why?

Here is the diff. Have you run cmake-format on code? If so, the version of cmake-format could make a difference.

https://github.com/open-telemetry/opentelemetry-cpp/actions/runs/4106845839/jobs/7085592898

@marcalff
Copy link
Member

@bachittle

Please fix the remaining CI failure about cmake-format.

This can be done by:

  • (1) installing and running cmake-format,
  • (2) or, copy and apply the patch given by the format failure in the build logs,
  • (3) or even manually change the code just by reading the diff from the logs, which is what I personally do most of the times.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c3c8b1e..b7cfe62 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -247,8 +247,8 @@ function(install_windows_deps)
   execute_process(
     COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tools/setup-buildtools.cmd)
   set(CMAKE_TOOLCHAIN_FILE
-  ${CMAKE_CURRENT_SOURCE_DIR}/tools/vcpkg/scripts/buildsystems/vcpkg.cmake
-  CACHE FILEPATH "")
+      ${CMAKE_CURRENT_SOURCE_DIR}/tools/vcpkg/scripts/buildsystems/vcpkg.cmake
+      CACHE FILEPATH "")
   message(
     "Make sure that vcpkg.cmake is set as the CMAKE_TOOLCHAIN_FILE at the START of the cmake build process!
     Can be command-line arg (cmake -DCMAKE_TOOLCHAIN_FILE=...) or set in your editor of choice."

In this case, there are just two lines to adjust.

Copy link
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

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

Almost there, cmake-format needs 2 more spaces.

CMakeLists.txt Outdated Show resolved Hide resolved
@marcalff marcalff changed the title fixing CMake to build GTest on Windows [BUILD] Fixing CMake to build GTest on Windows May 26, 2023
@marcalff marcalff merged commit 3707bc0 into open-telemetry:main May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants