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

Update GoogleTest and install it via FetchContent #4092

Merged
merged 2 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[submodule "test/frameworks/gtest"]
path = test/frameworks/gtest
url = https://github.com/google/googletest.git
ignored = untracked
[submodule "backends/ebpf/runtime/contrib/libbpf"]
path = backends/ebpf/runtime/contrib/libbpf
url = https://github.com/libbpf/libbpf
Expand Down
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ include_directories (
${P4C_SOURCE_DIR}
${P4C_BINARY_DIR}
)
include_directories(SYSTEM ${P4C_SOURCE_DIR}/test/frameworks/gtest/googletest/include)
add_definitions (-DCONFIG_PREFIX="${CMAKE_INSTALL_PREFIX}")
add_definitions (-DCONFIG_PKGDATADIR="${CMAKE_INSTALL_PREFIX}/${P4C_ARTIFACTS_OUTPUT_DIRECTORY}")

Expand All @@ -346,15 +345,21 @@ configure_file ("${P4C_SOURCE_DIR}/cmake/config.h.cmake" "${P4C_BINARY_DIR}/conf
set (IR_GENERATOR_DIRECTORY ${P4C_BINARY_DIR}/tools/ir-generator)
set (IR_GENERATOR ${IR_GENERATOR_DIRECTORY}/irgenerator)

# the order of adding subdirectories matters because of target dependencies
# Component libraries: must be defined before being used in the back end executables.
set (P4C_LIBRARIES controlplane midend frontend ir p4ctoolkit ir-generated)

# If we enable GTest, install GoogleTest with the appropriate options.
if (ENABLE_GTESTS)
include(GoogleTest)
p4c_obtain_googletest()
endif ()

# The order of adding subdirectories matters because of target dependencies.
add_subdirectory (tools/driver)
add_subdirectory (lib)
add_subdirectory (tools/ir-generator)
add_subdirectory (ir)

# component libraries: must be defined before being used in the
# backend executables
set (P4C_LIBRARIES controlplane midend frontend ir p4ctoolkit ir-generated)

# add extensions - before the frontends as they produce IR and extra frontend sources
set(EXTENSION_IR_SOURCES)
Expand All @@ -375,7 +380,6 @@ FetchContent_Declare(
GIT_REPOSITORY https://github.com/p4lang/p4runtime.git
GIT_TAG d76a3640a223f47a43dc34e5565b72e43796ba57
GIT_PROGRESS TRUE
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(p4runtime)
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV})
Expand Down
4 changes: 2 additions & 2 deletions backends/bmv2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ else()
endif()

set (GTEST_BMV2_SOURCES
)

gtest/load_ir_from_json.cpp
)
set (GTEST_SOURCES ${GTEST_SOURCES} ${GTEST_BMV2_SOURCES} PARENT_SCOPE)
set (GTEST_LDADD ${GTEST_LDADD} bmv2backend PARENT_SCOPE)
3 changes: 0 additions & 3 deletions backends/dpdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/version.h" @ONLY)

# For files generated by the protobuf compiler
include_directories(${P4C_BINARY_DIR}/control-plane)

################ Proto
set (DPDK_P4RUNTIME_DIR ${CMAKE_CURRENT_SOURCE_DIR}/control-plane/proto)
set (DPDK_P4RUNTIME_INFO_PROTO ${DPDK_P4RUNTIME_DIR}/p4info.proto)
Expand Down
1 change: 0 additions & 1 deletion backends/p4tools/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ target_include_directories(
SYSTEM BEFORE PUBLIC ${P4TOOLS_Z3_INCLUDE_DIR}
PUBLIC "${CMAKE_BINARY_DIR}/common"
PUBLIC "${P4C_SOURCE_DIR}"
PUBLIC "${P4C_SOURCE_DIR}/test/frameworks/gtest/googletest/include"
PUBLIC "${P4C_BINARY_DIR}"
)

Expand Down
7 changes: 0 additions & 7 deletions backends/p4tools/modules/testgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,16 @@ add_dependencies(p4testgen linkp4testgen)

if(ENABLE_GTESTS)
add_executable(testgen-gtest ${TESTGEN_GTEST_SOURCES})
target_include_directories(
testgen-gtest
PRIVATE ${P4C_SOURCE_DIR}/test/frameworks/gtest/googlemock/include
PRIVATE ${P4C_SOURCE_DIR}/test/frameworks/gtest/googletest/include
)
target_link_libraries(
testgen-gtest
PRIVATE testgen
PRIVATE gtest
${TESTGEN_LIBS}
${P4C_LIBRARIES}
${P4C_LIB_DEPS}
${CMAKE_THREAD_LIBS_INIT}
)

if(ENABLE_TESTING)
add_definitions("-DGTEST_HAS_PTHREAD=0")
add_test(NAME testgen-gtest COMMAND testgen-gtest)
set_tests_properties(testgen-gtest PROPERTIES LABELS "gtest-testgen")
endif()
Expand Down
3 changes: 2 additions & 1 deletion backends/p4tools/modules/testgen/test/gtest_utils.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_
#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_GTEST_UTILS_H_

#include <gtest/gtest.h>

#include <optional>
#include <string>

#include "backends/p4tools/common/lib/variables.h"
#include "frontends/common/options.h"
#include "ir/ir.h"
#include "test/frameworks/gtest/googletest/include/gtest/gtest.h"

namespace Test {

Expand Down
24 changes: 24 additions & 0 deletions cmake/GoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
macro(p4c_obtain_googletest)
# Some P4C source files include gtest files.
# We need this definition to guard against compilation errors.
add_definitions(-DP4C_GTEST_ENABLED)
# Print download state while setting up GTest.
set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET})
set(FETCHCONTENT_QUIET OFF)
set(INSTALL_GTEST OFF CACHE BOOL "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.")
set(gtest_disable_pthreads ON CACHE BOOL "Disable uses of pthreads in gtest.")
# Fetch and build the GTest dependency.
FetchContent_Declare(
gtest
GIT_REPOSITORY https://github.com/google/googletest.git
# https://github.com/google/googletest/releases/tag/v1.14.0
GIT_TAG f8d7d77c06936315286eb55f8de22cd23c188571
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(gtest)
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV})
message("Done with setting up GoogleTest for P4C.")

include_directories(BEFORE SYSTEM ${gtest_SOURCE_DIR}/googletest/include)
include_directories(BEFORE SYSTEM ${gtest_SOURCE_DIR}/googlemock/include)
endmacro(p4c_obtain_googletest)
12 changes: 7 additions & 5 deletions control-plane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ target_include_directories(controlplane-gen
SYSTEM BEFORE PUBLIC ${Protobuf_INCLUDE_DIR}
)
target_link_libraries (controlplane-gen PUBLIC ${PROTOBUF_LIBRARY})

# Needed for the correct import of google/status.pb.cc
include_directories (${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(
controlplane-gen PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
)


# Silence various warnings as the root issue is out of our control, example https://github.com/protocolbuffers/protobuf/issues/7140
set_source_files_properties (${P4RUNTIME_GEN_SRCS} {$P4RUNTIME_GEN_HDRS} PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter -Wno-array-bounds -Wno-error")

add_library (controlplane STATIC ${CONTROLPLANE_SRCS} )
target_link_libraries (controlplane ${CMAKE_THREAD_LIBS_INIT} controlplane-gen)
add_dependencies (controlplane mkP4configdir ir-generated controlplane-gen frontend-parser-gen)
add_library(controlplane STATIC ${CONTROLPLANE_SRCS})
target_link_libraries(controlplane ${CMAKE_THREAD_LIBS_INIT} controlplane-gen)
add_dependencies(controlplane mkP4configdir ir-generated controlplane-gen frontend-parser-gen)
4 changes: 4 additions & 0 deletions lib/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ limitations under the License.
#include <type_traits>
#include <vector>

#ifdef P4C_GTEST_ENABLED
#include "gtest/gtest_prod.h"
#endif
#include "lib/big_int_util.h"
#include "lib/castable.h"
#include "lib/cstring.h"
Expand All @@ -43,7 +45,9 @@ class IJson : public ICastable {
};

class JsonValue final : public IJson {
#ifdef P4C_GTEST_ENABLED
FRIEND_TEST(Util, Json);
#endif

public:
enum Kind { String, Number, True, False, Null };
Expand Down
4 changes: 4 additions & 0 deletions lib/source_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ limitations under the License.
#include "stringref.h"

// GTest
#ifdef P4C_GTEST_ENABLED
#include "gtest/gtest_prod.h"
#endif

namespace Test {
class UtilSourceFile;
Expand Down Expand Up @@ -261,7 +263,9 @@ class Comment final : IHasDbPrint {
This class implements a singleton pattern: there is a single instance of this class.
*/
class InputSources final {
#ifdef P4C_GTEST_ENABLED
FRIEND_TEST(UtilSourceFile, InputSources);
#endif

public:
InputSources();
Expand Down
26 changes: 2 additions & 24 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@
# GTests
################################################################################

# Flags needed for GTest tests.
set (GTEST_ROOT ${P4C_SOURCE_DIR}/test/frameworks/gtest/googletest)

include_directories(BEFORE ${GTEST_ROOT} ${GTEST_ROOT}/include
${P4C_SOURCE_DIR}/control-plane/PI/include)
# For files generated by the protobuf compiler
include_directories(${P4C_BINARY_DIR}/control-plane)

# Configure GTest to disable support for SEH (which is irrelevant for p4c since
# we don't currently support Windows) and pthreads (which prevents GTest from
# using its internal TLS implementation, which interacts badly with libgc).
add_definitions(-DGTEST_HAS_SEH=0)
add_definitions(-DGTEST_HAS_PTHREAD=0)

# Build the GTest library itself.
add_library(gtest ${P4C_STATIC_BUILD} ${GTEST_ROOT}/src/gtest-all.cc)

set (GTEST_UNITTEST_SOURCES
gtest/arch_test.cpp
gtest/bitvec_test.cpp
Expand Down Expand Up @@ -60,13 +43,7 @@ set (GTEST_UNITTEST_SOURCES
gtest/source_file_test.cpp
gtest/transforms.cpp
gtest/stringify.cpp
)
if (ENABLE_BMV2)
set (GTEST_UNITTEST_SOURCES ${GTEST_UNITTEST_SOURCES} gtest/load_ir_from_json.cpp)
endif()
set (GTEST_UNITTEST_HEADERS
gtest/helpers.h
)
)

# Combine the executable and the non-backend-specific unit tests into a single
# unity compilation group.
Expand All @@ -92,6 +69,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/test)
configure_file(gtest/env.h.in ${CMAKE_CURRENT_BINARY_DIR}/gtest/env.h)
add_executable (gtestp4c ${GTESTP4C_SOURCES})
target_link_libraries (gtestp4c ${GTEST_LDADD} ${P4C_LIBRARIES} gtest ${P4C_LIB_DEPS})
add_dependencies(gtestp4c gtest genIR frontend controlplane)

# Tests
add_test (NAME gtestp4c COMMAND gtestp4c WORKING_DIRECTORY ${P4C_BINARY_DIR})
Expand Down
1 change: 0 additions & 1 deletion test/frameworks/gtest
Submodule gtest deleted from aa148e
4 changes: 2 additions & 2 deletions test/gtest/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ limitations under the License.
#include "frontends/p4/frontend.h"
#include "frontends/p4/parseAnnotations.h"

namespace detail {
namespace TestDetail {

std::string makeP4Source(const char *file, unsigned line, P4Headers headers,
const char *rawSource) {
Expand Down Expand Up @@ -85,7 +85,7 @@ std::string makeP4Source(const char *file, unsigned line, const char *rawSource)
return makeP4Source(file, line, P4Headers::NONE, rawSource);
}

} // namespace detail
} // namespace TestDetail

/* static */ P4CTestEnvironment *P4CTestEnvironment::get() {
static P4CTestEnvironment *instance = new P4CTestEnvironment;
Expand Down
6 changes: 3 additions & 3 deletions test/gtest/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum class P4Headers {
PSA // Both core.p4 and psa.p4
};

namespace detail {
namespace TestDetail {

/**
* Transforms the P4 program (or program fragment) in @rawSource to turn it into
Expand All @@ -59,12 +59,12 @@ std::string makeP4Source(const char *file, unsigned line, P4Headers headers, con
/// `makeP4Source(file, line, P4Headers::NONE, rawSource);`.
std::string makeP4Source(const char *file, unsigned line, const char *rawSource);

} // namespace detail
} // namespace TestDetail

// A macro which should be used by unit tests to define P4 source code. It adds
// additional information to the source code to aid in debugging; see
// makeP4Source for more information and parameter details.
#define P4_SOURCE(...) detail::makeP4Source(__FILE__, __LINE__, __VA_ARGS__)
#define P4_SOURCE(...) TestDetail::makeP4Source(__FILE__, __LINE__, __VA_ARGS__)

class P4CTestEnvironment {
// XXX(seth): Ideally this would be a ::testing::Environment subclass, but
Expand Down
Loading