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

add macro to assist in checking if code snippets compile #600

Merged
merged 5 commits into from
Oct 4, 2022

Conversation

white238
Copy link
Member

@white238 white238 commented Oct 1, 2022

We ran into a problem where Umpire was trying to use CMake's macro check_cxx_source_compiles. We found that it did not honor the CMAKE_CXX_STANDARD. There is a policy to control this but I could only get it to work for CMake 3.14.5 and 3.18.0 but anything above that did not work.

This adds a macro we have used in Axom for a while. It is much more reliable and also allows you to output verbosely to the screen. For example:

-- [blt_check_code_compiles] Attempting to compile source string: 
#include <iostream>

  int main(int, char**)
  {

    std::cout << "Hello World!" << std::endl;

    return 0;
  }
-- [blt_check_code_compiles] Compiler output: 
Change Dir: /usr/WS2/white238/blt/repo/tests/internal/build/unit/CMakeTmp/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/gmake -f Makefile cmTC_5e24b/fast && /usr/bin/gmake  -f CMakeFiles/cmTC_5e24b.dir/build.make CMakeFiles/cmTC_5e24b.dir/build
gmake[1]: Entering directory `/usr/WS2/white238/blt/repo/tests/internal/build/unit/CMakeTmp/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_5e24b.dir/_bltCheckCompilesykiyL.cpp.o
/usr/tce/packages/gcc/gcc-10.2.1/bin/c++   -Wall -Wextra       -fPIE -std=c++11 -o CMakeFiles/cmTC_5e24b.dir/_bltCheckCompilesykiyL.cpp.o -c /usr/WS2/white238/blt/repo/tests/internal/build/unit/_bltCheckCompilesykiyL.cpp
Linking CXX executable cmTC_5e24b
/usr/tce/packages/cmake/cmake-3.20.2/bin/cmake -E cmake_link_script CMakeFiles/cmTC_5e24b.dir/link.txt --verbose=1
/usr/tce/packages/gcc/gcc-10.2.1/bin/c++  -Wall -Wextra       CMakeFiles/cmTC_5e24b.dir/_bltCheckCompilesykiyL.cpp.o -o cmTC_5e24b 
gmake[1]: Leaving directory `/usr/WS2/white238/blt/repo/tests/internal/build/unit/CMakeTmp/CMakeFiles/CMakeTmp'



-- [blt_check_code_compiles] The code snippet successfully compiled

@white238 white238 added bug feature documentation Issues related to documentation flags Issues related to setting compiler flags C++ Related to C++ User Request Related to user requests labels Oct 1, 2022
Copy link
Contributor

@mcfadden8 mcfadden8 left a comment

Choose a reason for hiding this comment

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

This looks great @white238 - I tried out the branch locally with my Umpire tests and all seems good.

@eugeneswalker
Copy link

eugeneswalker commented Oct 3, 2022

This looks great @white238 - I tried out the branch locally with my Umpire tests and all seems good.

This fixes spack/spack#32932 for me

@white238 white238 force-pushed the feature/white238/add_try_compile branch from 4a37030 to 02a36a2 Compare October 4, 2022 01:18
@white238 white238 force-pushed the feature/white238/add_try_compile branch from 02a36a2 to 7901598 Compare October 4, 2022 01:22
Copy link
Member

@kennyweiss kennyweiss left a comment

Choose a reason for hiding this comment

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

Thanks @white238 !

Comment on lines 159 to 168
"#include <iostream>

int main(int, char**)
{

std::cout << \"Hello World!\" << std::endl;

return 0;
}
")
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: Should we use a "bracket argument" (multiline string) instead of a single quote string?
One benefit would be that we wouldn't need to modify the quotation marks
e,g,:

[=[
#include <iostream>

int main(int, char**)
{
    std::cout << \"Hello World!\" << std::endl;
    return 0;
}
]=]

See: https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#bracket-argument

Copy link
Member Author

Choose a reason for hiding this comment

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

This worked.. added it as a code snippet in the documentation as well.


set(options)
set(singleValueArgs CODE_COMPILES VERBOSE_OUTPUT)
set(multiValueArgs SOURCE_STRING)
Copy link
Member

Choose a reason for hiding this comment

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

Does SOURCE_STRING need to be a multiValueArg because it can have embedded semicolons?

Copy link
Member Author

Choose a reason for hiding this comment

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

i bet it shouldn't actually.

Copy link
Member Author

Choose a reason for hiding this comment

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

Tried changing this to a single value arg and it removed all the semi-colons.

@white238
Copy link
Member Author

white238 commented Oct 4, 2022

https://llnl-blt.readthedocs.io/en/feature-white238-add_try_compile/api/utility.html#blt-check-code-compiles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug C++ Related to C++ documentation Issues related to documentation feature flags Issues related to setting compiler flags User Request Related to user requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants