Skip to content

Commit

Permalink
Depend on Boost using FetchContent instead of relying on system-provi…
Browse files Browse the repository at this point in the history
…ded Boost.
  • Loading branch information
fruffy committed May 10, 2024
1 parent 8edd039 commit b224bff
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 40 deletions.
29 changes: 9 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ if(STATIC_BUILD_WITH_DYNAMIC_GLIBC OR STATIC_BUILD_WITH_DYNAMIC_STDLIB)
endif()
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
# Link Boost statically
# See https://cmake.org/cmake/help/latest/module/FindBoost.html for details
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME OFF)
# Set the static variable
set(P4C_STATIC_BUILD STATIC)
# TODO: We can not use -static here because of compilation and portability problems:
Expand Down Expand Up @@ -197,15 +193,8 @@ include(Abseil)
p4c_obtain_abseil()
include(Protobuf)
p4c_obtain_protobuf()

# The boost graph headers are optional and only required by the graphs back end.
find_package (Boost QUIET COMPONENTS graph)
if (Boost_FOUND)
set (HAVE_LIBBOOST_GRAPH 1)
else ()
message (WARNING "Boost graph headers not found, will not build 'graphs' backend")
endif ()
find_package (Boost REQUIRED COMPONENTS iostreams)
include(Boost)
p4c_obtain_boost()

if (ENABLE_GC)
find_package (LibGc 7.4.2 REQUIRED)
Expand All @@ -218,16 +207,16 @@ endif()
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
list (APPEND P4C_LIB_DEPS ${CMAKE_THREAD_LIBS_INIT})
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIRS})
include_directories(SYSTEM ${LIBGC_INCLUDE_DIR})
set (HAVE_LIBBOOST_IOSTREAMS 1)
list (APPEND P4C_LIB_DEPS ${Boost_LIBRARIES})
include_directories(BEFORE SYSTEM ${PROTOBUF_INCLUDE_DIRS})
include_directories(BEFORE SYSTEM ${LIBGC_INCLUDE_DIR})
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)

if (ENABLE_GC)
list (APPEND P4C_LIB_DEPS ${LIBGC_LIBRARIES})
endif ()
set (P4C_ABSL_LIBRARIES absl::flat_hash_map absl::flat_hash_set)
list (APPEND P4C_LIB_DEPS ${P4C_ABSL_LIBRARIES})
list(APPEND P4C_LIB_DEPS Boost::iostreams Boost::format Boost::multiprecision)

# other required libraries
p4c_add_library (rt clock_gettime HAVE_CLOCK_GETTIME)
Expand All @@ -241,7 +230,7 @@ check_include_file(backtrace-supported.h HAVE_LIBBACKTRACE)
check_include_file_cxx(mm_malloc.h HAVE_MM_MALLOC_H)
check_include_file_cxx(cxxabi.h HAVE_CXXABI_H)
if (HAVE_LIBBACKTRACE)
set (P4C_LIB_DEPS "${P4C_LIB_DEPS};-lbacktrace")
set (P4C_LIB_DEPS "${P4C_LIB_DEPS};-lbacktrace")
endif ()

# check functions
Expand Down Expand Up @@ -464,7 +453,7 @@ endif ()
if (ENABLE_EBPF)
add_subdirectory (backends/ebpf)
endif ()
if (ENABLE_P4C_GRAPHS AND HAVE_LIBBOOST_GRAPH EQUAL 1)
if (ENABLE_P4C_GRAPHS)
add_subdirectory (backends/graphs)
endif ()
if (ENABLE_P4TC)
Expand Down
12 changes: 12 additions & 0 deletions backends/graphs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# fetchcontent_declare(
# boost_graph
# GIT_REPOSITORY https://github.com/boostorg/graph.git
# GIT_TAG boost-${P4C_BOOST_VERSION}
# USES_TERMINAL_DOWNLOAD TRUE
# GIT_PROGRESS TRUE
# GIT_SHALLOW TRUE
# )
# include_directories(BEFORE SYSTEM ${boost_graph_SOURCE_DIR}/include)
# fetchcontent_makeavailable(boost_graph)


configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/version.h" @ONLY)

Expand Down
2 changes: 0 additions & 2 deletions backends/p4tools/common/lib/symbolic_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <algorithm>
#include <utility>

#include <boost/container/vector.hpp>

#include "backends/p4tools/common/lib/model.h"
#include "ir/indexed_vector.h"
#include "ir/vector.h"
Expand Down
76 changes: 76 additions & 0 deletions cmake/Boost.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
macro(p4c_obtain_boost)
option(
P4C_USE_PREINSTALLED_BOOST
"Look for a preinstalled version of Boost in the system instead of installing the library using FetchContent."
OFF
)

set(P4C_BOOST_VERSION "1.85.0")

# If P4C_USE_PREINSTALLED_BOOST is ON just try to find a preinstalled version of Boost.
if(P4C_USE_PREINSTALLED_BOOST)
if(NOT BUILD_SHARED_LIBS)
# Link Boost statically
# See https://cmake.org/cmake/help/latest/module/FindBoost.html for details
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME OFF)
endif()

# The boost graph headers are optional and only required by the graphs back end.
find_package (Boost QUIET COMPONENTS graph)
if (Boost_FOUND)
set (HAVE_LIBBOOST_GRAPH 1)
else ()
message (WARNING "Boost graph headers not found, will not build 'graphs' backend")
endif ()
find_package (Boost REQUIRED COMPONENTS format multiprecision)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})

else()
message(STATUS "Fetching Boost version ${P4C_BOOST_VERSION} for P4C...")
# Print out download state while setting up Boost.
set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET})
set(FETCHCONTENT_QUIET OFF)
# Unity builds do not work for Abseil...
set(CMAKE_UNITY_BUILD_PREV ${CMAKE_UNITY_BUILD})
set(CMAKE_UNITY_BUILD OFF)


# Add boost library sources.
set(BOOST_INCLUDE_LIBRARIES graph format multiprecision iostreams)
set(BOOST_ENABLE_CMAKE ON)

if (ENABLE_P4C_GRAPHS)
set(HAVE_LIBBOOST_GRAPH 1)
endif()

# Always link Boost statically.
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_MP_STANDALONE ON CACHE BOOL "Use Boost.Multiprecision in standalone mode")

# Download and extract the boost library from GitHub.
message(STATUS "Downloading and extracting boost library sources. This may take some time...")
FetchContent_Declare(
Boost
URL https://github.com/boostorg/boost/releases/download/boost-${P4C_BOOST_VERSION}/boost-${P4C_BOOST_VERSION}-cmake.7z
USES_TERMINAL_DOWNLOAD TRUE
GIT_PROGRESS TRUE
DOWNLOAD_NO_EXTRACT FALSE
)
fetchcontent_makeavailable_but_exclude_install(Boost)
include_directories(BEFORE SYSTEM ${Boost_SOURCE_DIR}/libs/iostreams/include)
include_directories(BEFORE SYSTEM ${Boost_SOURCE_DIR}/libs/format/include)
include_directories(BEFORE SYSTEM ${Boost_SOURCE_DIR}/libs/multiprecision/include)
include_directories(BEFORE SYSTEM ${Boost_SOURCE_DIR}/libs/graph/include)


# Reset temporary variable modifications.
set(CMAKE_UNITY_BUILD ${CMAKE_UNITY_BUILD_PREV})
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV})
endif()

set (HAVE_LIBBOOST_IOSTREAMS TRUE)

message(STATUS "Done with setting up Boost for P4C.")
endmacro(p4c_obtain_boost)
3 changes: 2 additions & 1 deletion frontends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,5 @@ target_link_libraries (frontend
PRIVATE frontend-parser-gen
PRIVATE absl::strings
PUBLIC absl::flat_hash_set
PUBLIC absl::flat_hash_map)
PUBLIC absl::flat_hash_map
)
14 changes: 8 additions & 6 deletions ir/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#include <optional>
#include <vector>

#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "ir/ir.h"
#include "lib/castable.h"
#include "lib/cstring.h"
Expand All @@ -20,13 +19,16 @@ struct SymbolicVarComp {
bool operator()(const IR::SymbolicVariable *s1, const IR::SymbolicVariable *s2) const {
return s1->operator<(*s2);
}
size_t operator()(const IR::SymbolicVariable *s1) const {
return std::hash<cstring>()(s1->label);
}
};

/// This type maps symbolic variables to their value assigned by the solver.
using SymbolicMapping = boost::container::flat_map<const IR::SymbolicVariable *,
const IR::Expression *, SymbolicVarComp>;
using SymbolicMapping =
absl::flat_hash_map<const IR::SymbolicVariable *, const IR::Expression *, SymbolicVarComp>;

using SymbolicSet = boost::container::flat_set<const IR::SymbolicVariable *, SymbolicVarComp>;
using SymbolicSet = absl::flat_hash_set<const IR::SymbolicVariable *, SymbolicVarComp>;

/// Provides a higher-level interface for an SMT solver.
class AbstractSolver : public ICastable {
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ add_library(p4ctoolkit STATIC ${LIBP4CTOOLKIT_SRCS})

# Disable libcall (realloc, malloc) optimizations which may cause infinite loops.
set_target_properties(p4ctoolkit PROPERTIES COMPILE_FLAGS -fno-builtin)
target_link_libraries(p4ctoolkit absl::bits)
target_link_libraries(p4ctoolkit PUBLIC absl::bits)
3 changes: 0 additions & 3 deletions tools/ci-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ P4C_DEPS="bison \
g++ \
git \
lld \
libboost-dev \
libboost-graph-dev \
libboost-iostreams-dev \
libfl-dev \
libgc-dev \
pkg-config \
Expand Down
7 changes: 0 additions & 7 deletions tools/install_mac_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,14 @@ HOMEBREW_PREFIX=$(brew --prefix)
# Fetch the latest formulae
brew update

BOOST_LIB="[email protected]"
REQUIRED_PACKAGES=(
autoconf automake bdw-gc ccache cmake libtool
openssl pkg-config coreutils bison grep ninja
${BOOST_LIB}
)
for package in "${REQUIRED_PACKAGES[@]}"; do
brew_install ${package}
done

# Check if linking is needed.
if ! brew ls --linked --formula ${BOOST_LIB} > /dev/null 2>&1; then
brew link ${BOOST_LIB}
fi

# Check if PATH modification is needed.
if ! grep -q "$(brew --prefix bison)/bin" ~/.bash_profile; then
echo 'export PATH="$(brew --prefix bison)/bin:$PATH"' >> ~/.bash_profile
Expand Down

0 comments on commit b224bff

Please sign in to comment.