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 CMake configuration for test/googletest and test/unit #1034

Merged
merged 1 commit into from
Jun 7, 2018
Merged

Add CMake configuration for test/googletest and test/unit #1034

merged 1 commit into from
Jun 7, 2018

Conversation

mloskot
Copy link
Member

@mloskot mloskot commented Jun 7, 2018

For CMake builds, latest googletest 1.8.0 is downloaded.

Following officially recommended integration for CMake-enabled projects
https://github.com/google/googletest/blob/master/googletest/README.md

"Use CMake to download GoogleTest as part of the build's configure step.
This is just a little more complex, but doesn't have the limitations
of the other methods."

Since, our copy of test/googletest

  • is a very minimalist copy of googletest
  • does not include any official CMake scripts
  • would require copying parts of googletest CMakeLists.txt, compilater/linker flags (e.g. -lpthreads)

for reliable multi-platform builds, it is reasoanable to rely on download
All pros and cons advantages are discussed in teh README.md linked above.

Closes #1033


Pity GNU autotools does not support the download mode.


Sample session to build and run proj, googletest, and proj tests

  • CMake step
$ cmake ..
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
--
-- Configuring PROJ:
--
-- PROJ_VERSION                             = 5.0.0
-- Looking for include file dlfcn.h
-- Looking for include file dlfcn.h - found
-- Looking for include file inttypes.h
-- Looking for include file inttypes.h - found
-- Looking for include file jni.h
-- Looking for include file jni.h - not found
-- Looking for include file memory.h
-- Looking for include file memory.h - found
-- Looking for include file stdint.h
-- Looking for include file stdint.h - found
-- Looking for include file stdlib.h
-- Looking for include file stdlib.h - found
-- Looking for include file string.h
-- Looking for include file string.h - found
-- Looking for include file sys/stat.h
-- Looking for include file sys/stat.h - found
-- Looking for include file sys/types.h
-- Looking for include file sys/types.h - found
-- Looking for include file unistd.h
-- Looking for include file unistd.h - found
-- Looking for 4 include files stdlib.h, ..., float.h
-- Looking for 4 include files stdlib.h, ..., float.h - found
-- Looking for localeconv
-- Looking for localeconv - found
-- Looking for ceil in m
-- Looking for ceil in m - found
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Looking for PTHREAD_MUTEX_RECURSIVE
-- Looking for PTHREAD_MUTEX_RECURSIVE - not found
-- Performing Test C99_MATH
-- Performing Test C99_MATH - Success
-- PROJ_PLATFORM_NAME                       = x64
-- PROJ_COMPILER_NAME                       = gcc-7
-- PROJ_TESTS                               = ON
--
-- Configuring proj library:
--
-- JNI_SUPPORT                              = OFF
-- PROJ_CORE_TARGET                         = proj
-- PROJ_CORE_TARGET_OUTPUT_NAME             = proj
-- PROJ_LIBRARY_TYPE                        = SHARED
-- PROJ_LIBRARIES                           = proj
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/d/proj.4/_build.gcc7/googletest-download
Scanning dependencies of target googletest
[ 11%] Creating directories for 'googletest'
[ 22%] Performing download step (git clone) for 'googletest'
Cloning into 'googletest-src'...
Already on 'master'
Your branch is up-to-date with 'origin/master'.
[ 33%] No patch step for 'googletest'
[ 44%] Performing update step for 'googletest'
Current branch master is up to date.
[ 55%] No configure step for 'googletest'
[ 66%] No build step for 'googletest'
[ 77%] No install step for 'googletest'
[ 88%] No test step for 'googletest'
[100%] Completed 'googletest'
[100%] Built target googletest
-- Found PythonInterp: /usr/bin/python (found version "2.7.12")
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/d/proj.4/_build.gcc7
  • Build step
$ cmake --build .
Scanning dependencies of target proj
[  0%] Building C object src/CMakeFiles/proj.dir/nad_init.c.o
...
Scanning dependencies of target gtest
[ 96%] Building CXX object googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 97%] Linking CXX static library ../../../lib/libgtest.a
[ 97%] Built target gtest
Scanning dependencies of target proj_test_unit
[ 98%] Building CXX object test/unit/CMakeFiles/proj_test_unit.dir/main.cpp.o
[ 98%] Building CXX object test/unit/CMakeFiles/proj_test_unit.dir/basic_test.cpp.o
[100%] Linking CXX executable ../../bin/proj_test_unit
[100%] Built target proj_test_unit
  • Test step
$ ctest -R proj_test_unit
Test project /mnt/d/proj.4/_build.gcc7
    Start 30: proj_test_unit
1/1 Test #30: proj_test_unit ...................   Passed    0.02 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.04 sec

@mloskot mloskot changed the title Add CMake configuration for test/googletest and test/unit WIP: Add CMake configuration for test/googletest and test/unit Jun 7, 2018
@kbevers
Copy link
Member

kbevers commented Jun 7, 2018

for reliable multi-platform builds, it is reasoanable to rely on download

So, CMake downloads a copy of googletest when configuring, yes? Which means we are likely to be using different version across autotools and CMake. What are the chances that'll go wrong?

@rouault
Copy link
Member

rouault commented Jun 7, 2018

I see a GIT_TAG master, we could probably change that to "release-1.8.0" to match the copy in PROJ ?

Unrelated the clang builds fail with

In file included from /home/travis/build/OSGeo/proj.4/build_cmake/googletest-src/googletest/src/gtest-all.cc:42:
/home/travis/build/OSGeo/proj.4/build_cmake/googletest-src/googletest/src/gtest.cc:392:12: error: missing field 'owner_' initializer [-Werror,-Wmissing-field-initializers]
GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_linked_ptr_mutex);
           ^
/home/travis/build/OSGeo/proj.4/build_cmake/googletest-src/googletest/include/gtest/internal/gtest-port.h:2096:80: note: expanded from macro 'GTEST_DEFINE_STATIC_MUTEX_'
     ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }

I believe the -Wmissing-field-initializers flag is an expansion from the -Wextra flag added to CMAKE_CXX_FLAGS by our main CMakeList.txt. Perhaps that the compilation of gtest shouldn't use those hardened flags ? (I had to do similar things for the automake build)

For CMake builds, latest googletest 1.8.0 is downloaded.

Following officially recommended integration for CMake-enabled projects
https://github.com/google/googletest/blob/master/googletest/README.md

  "Use CMake to download GoogleTest as part of the build's configure step.
  This is just a little more complex, but doesn't have the limitations
  of the other methods."

Since, our copy of test/googletest
- is a very minimalist copy of googletest
- does not include any official CMake scripts
- would require copying parts of googletest CMakeLists.txt, compilater/linker flags (e.g. -lpthreads)
for reliable multi-platform builds, it is reasoanable to rely on download
All pros and cons advantages are discussed in teh README.md linked above.

Closes #1033
@mloskot
Copy link
Member Author

mloskot commented Jun 7, 2018

I have updated the PR with the following:

  • googletest 1.8.0 archive is downloaded, not the master
  • strip -W* flags from global CMake flags for googletest build.

The issue is, that our CMake configuration is written in old-school fashion and is behaving badly - it modifies global CMake flags. The current/modern CMake recommendation is to never do that. Instead, we should set any compiler/linker flags/defines per target, not globally or per directory.

@kbevers

we are likely to be using different version across autotools and CMake.
What are the chances that'll go wrong?

Obivously, I can't speak for googletest, but I wouldn't expect a terrible breakage between releases. Perhaps @schwehr could tell more. However, I've pinpointed the download at googletest 1.8.0. This should ensure googletest 'constness'.

@rouault
Regarding googletest issue with -Wmissing-field-initializers, there seem to be regression, fixed in 1.8.0 (see google/googletest#1521). AFAICT, this update should be fine now - here are flags used to compile googletest and our unit test:

cmake -DCMAKE_VERBOSE_MAKEFILE=ON ..
...
cmake --build .
...
[ 97%] Building CXX object googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
... -std=c++11 -Wall -Wshadow -DGTEST_HAS_PTHREAD=1 -fexceptions -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
...
[ 98%] Building CXX object test/unit/CMakeFiles/proj_test_unit.dir/main.cpp.o
... -std=c++11 -Wall -Wextra -Wswitch -Wshadow -Wunused-parameter -Wmissing-declarations -Wformat -Wformat-security

@kbevers
Copy link
Member

kbevers commented Jun 7, 2018

Obivously, I can't speak for googletest, but I wouldn't expect a terrible breakage between releases. Perhaps @schwehr could tell more. However, I've pinpointed the download at googletest 1.8.0. This should ensure googletest 'constness'.

Okay, cool. I still don't really understand what the advantage of downloading googletest is when we already have it in the repo. Care to elaborate?

@mloskot
Copy link
Member Author

mloskot commented Jun 7, 2018

@kbevers

I still don't really understand what the advantage of downloading googletest is when we already have it in the repo

I hoped the reference to/quote from googletest README will explain.

Actually, we don't have it or we have it partially. We have minimal set of sources required to compile googletest library, but we don't have any googletest's CMakeLists.txt. It means, we don't have any googletest build requirements or any googletest usage requirements which are important especially for multi-platform builds. So, we would have to replicate googletest CMakeLists.txt configurations. For example, we have to replicate that on certain platforms googletest requires -lpthreads. Who knows what else googletest requires. Should we care? No.
PDAL, for example, does not want to care and hosts full googletest source package in its vendor directory - "the simplest approach" according to googletest README (linked in the issue and below). We dont want to care, so we achieve similar but different way - "just a little more complex, but doesn't have the limitations of the other methods"

This is explained in https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project

BTW, the download happens once, during CMake step. The (only?) disadvantage is that it requires internet connection. AFAIU, GNU autotools is still the main build configuration, so it shouldn't be a major issue, I guess.

@rouault
Copy link
Member

rouault commented Jun 7, 2018

Everything green now. I think this is good enough to be merged. Thanks @mloskot !

I didn't initially import the whole googletest archive to avoid littering too much proj code base with it, but that's something we could revise if needed. Anyway I think the download approach is OK for now.

@kbevers
Copy link
Member

kbevers commented Jun 7, 2018

I hoped the reference to/quote from googletest README will explain.

Sorry, I read it but didn't understand it. Probably just me that is daft. Your explanation here makes a lot more sense to me. Thanks. Seems like an okay approach to me.

@mloskot
Copy link
Member Author

mloskot commented Jun 7, 2018

@kbevers No worries, I'm glad I've managed to understand it myself and pass the knowledge through clearer than mud.

@rouault Understood, that is also why I favoured the download option. Yes, leaving revision of the setup as we go and need is a good idea, I think.

@mloskot mloskot changed the title WIP: Add CMake configuration for test/googletest and test/unit Add CMake configuration for test/googletest and test/unit Jun 7, 2018
@rouault rouault merged commit ffd4674 into OSGeo:master Jun 7, 2018
@mloskot mloskot deleted the ml/add-cmake-for-gtest branch June 8, 2018 07:21
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.

None yet

3 participants