Skip to content

Commit

Permalink
blah
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed Jan 18, 2024
1 parent ce4fa84 commit 7735281
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 19 deletions.
30 changes: 12 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ project( ClamBCC
DESCRIPTION "ClamAV Bytecode Compiler." )

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
#include(Version)
include(Version)

set(PACKAGE_NAME "${PROJECT_NAME}")
set(PACKAGE_VERSION "${PROJECT_VERSION}")
set(PACKAGE_STRING "${PROJECT_NAME} ${PROJECT_VERSION}${VERSION_SUFFIX}")
set(PACKAGE_BUGREPORT "https://github.com/Cisco-Talos/clamav-bytecode-compiler/issues")
set(PACKAGE_URL "https://www.clamav.net/")
#HexVersion(PACKAGE_VERSION_NUM ${PROJECT_VERSION_MAJOR} ${PROJECT_VERSION_MINOR} ${PROJECT_VERSION_PATCH})
HexVersion(PACKAGE_VERSION_NUM ${PROJECT_VERSION_MAJOR} ${PROJECT_VERSION_MINOR} ${PROJECT_VERSION_PATCH})

# libtool library versioning rules: http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
set(LIBCLAMBC_CURRENT 1)
Expand All @@ -40,7 +40,7 @@ set(LIBCLAMBC_AGE 0)

math(EXPR LIBCLAMBC_SOVERSION "${LIBCLAMBC_CURRENT} - ${LIBCLAMBC_AGE}")
set(LIBCLAMBC_VERSION "${LIBCLAMBC_SOVERSION}.${LIBCLAMBC_AGE}.${LIBCLAMBC_REVISION}")
#HexVersion(LIBCLAMBC_VERSION_NUM ${LIBCLAMBC_CURRENT} ${LIBCLAMBC_REVISION} ${LIBCLAMBC_AGE})
HexVersion(LIBCLAMBC_VERSION_NUM ${LIBCLAMBC_CURRENT} ${LIBCLAMBC_REVISION} ${LIBCLAMBC_AGE})

# Git optionally used to add commit info into build to differentiate in bug reports.
find_package(Git)
Expand Down Expand Up @@ -103,7 +103,7 @@ if(ENABLE_TESTS)
set(Python3_TEST_PACKAGE "pytest;-v")
endif()

#find_package(ClamAV REQUIRED)
find_package(ClamAV REQUIRED)
endif()

find_package(LLVM 16 REQUIRED)
Expand Down Expand Up @@ -187,15 +187,10 @@ configure_file(clambc-version.h.in clambc-version.h)
# Build targets!
#

include(AddLLVM)

# The bytecode compiler optimization passes
# This is the core of the bytecode compiler
add_subdirectory(libclambcc)

# Examples of plugins for the new and legacy pass managers.
add_subdirectory(examples)

# The bytecode compiler application
# This is really just a python script
add_subdirectory(clambcc)
Expand All @@ -217,18 +212,17 @@ add_subdirectory(headers)
# `pandoc -s file.tex -o file.md` mostly-works, but w/ the doxygen integration is insufficient.
# add_subdirectory(docs)

#if(ENABLE_EXAMPLES)
# # Example optimization passes; boilerplate to help compiler devs write new passes.
# add_subdirectory( examples )
#endif()
if(ENABLE_EXAMPLES)
# Example optimization passes; boilerplate to help compiler devs write new passes.
add_subdirectory( examples )
endif()

include(CTest)

add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
#if(ENABLE_TESTS)
# # Tests to verify compiler works as intended and that signatures behave as intended.
# add_subdirectory( test )
#endif()
if(ENABLE_TESTS)
# Tests to verify compiler works as intended and that signatures behave as intended.
add_subdirectory( test )
endif()

if(WIN32)
# Include the license(s) in the installation
Expand Down
94 changes: 94 additions & 0 deletions cmake/FindClamAV.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#
# Find the ClamAV programs and headers needed for the test suite.
#
# If found, will set:
# ClamAV_FOUND, ClamAV_VERSION, and
# - clamscan_EXECUTABLE
# - clambc_EXECUTABLE
# - sigtool_EXECUTABLE
# - clambc_headers_DIRECTORY
#
# If you have a custom install location for ClamAV, you can provide a hint
# by settings -DClamAV_HOME=<clamav install prefix>
#

find_program(clamscan_EXECUTABLE
NAMES clamscan clamscan.exe
HINTS "${ClamAV_HOME}"
PATH_SUFFIXES "bin"
)
if(NOT clamscan_EXECUTABLE AND NOT ClamAV_FIND_QUIETLY)
message("Unable to find clamscan")
endif()

find_program(clambc_EXECUTABLE
NAMES clambc clambc.exe
HINTS "${ClamAV_HOME}"
PATH_SUFFIXES "bin"
)
if(NOT clambc_EXECUTABLE AND NOT ClamAV_FIND_QUIETLY)
message("Unable to find clambc")
endif()

find_program(sigtool_EXECUTABLE
NAMES sigtool sigtool.exe
HINTS "${ClamAV_HOME}"
PATH_SUFFIXES "bin"
)
if(NOT sigtool_EXECUTABLE AND NOT ClamAV_FIND_QUIETLY)
message("Unable to find sigtool")
endif()

if(clamscan_EXECUTABLE AND clambc_EXECUTABLE AND sigtool_EXECUTABLE)
execute_process(COMMAND "${clamscan_EXECUTABLE}" --version
OUTPUT_VARIABLE ClamAV_VERSION_OUTPUT
ERROR_VARIABLE ClamAV_VERSION_ERROR
RESULT_VARIABLE ClamAV_VERSION_RESULT
)
if(NOT ${ClamAV_VERSION_RESULT} EQUAL 0)
if(NOT ClamAV_FIND_QUIETLY)
message(STATUS "ClamAV not found: Failed to determine version.")
endif()
unset(clamscan_EXECUTABLE)
else()
string(REGEX
MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?(-devel)?"
ClamAV_VERSION "${ClamAV_VERSION_OUTPUT}"
)
set(ClamAV_VERSION "${ClamAV_VERSION}")
set(ClamAV_FOUND 1)

# Look for the clambc-headers. E.g.: <clamav prefix>/lib/clambc-headers/0.104.0
#
# In the future, the clamav-derived headers for compiling signatures will be
# installed with clamav, and this path will be necessary to find them for running
# the test suite.
find_file(clambc_headers_DIRECTORY
clambc-headers/${ClamAV_VERSION}
HINTS "${ClamAV_HOME}"
PATH_SUFFIXES "lib"
)

if(NOT ClamAV_FIND_QUIETLY)
message(STATUS "ClamAV found: ${ClamAV_VERSION}")
message(STATUS " clamscan: ${clamscan_EXECUTABLE}")
message(STATUS " clambc: ${clambc_EXECUTABLE}")
message(STATUS " sigtool: ${sigtool_EXECUTABLE}")
message(STATUS " bc headers: ${clambc_headers_DIRECTORY}")
endif()

if(NOT clambc_headers_DIRECTORY)
set(clambc_headers_DIRECTORY "")
endif()
endif()

mark_as_advanced(clamscan_EXECUTABLE clambc_EXECUTABLE sigtool_EXECUTABLE ClamAV_VERSION)
else()
if(ClamAV_FIND_REQUIRED)
message(FATAL_ERROR "ClamAV not found.")
else()
if(NOT ClamAV_FIND_QUIETLY)
message(STATUS "${_msg}")
endif()
endif()
endif()
177 changes: 177 additions & 0 deletions cmake/FindClang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Detect Clang libraries
#
# Defines the following variables:
# CLANG_FOUND - True if Clang was found
# CLANG_INCLUDE_DIRS - Where to find Clang includes
# CLANG_LIBRARY_DIRS - Where to find Clang libraries
# CLANG_BUILTIN_DIR - Where to find Clang builtin includes
#
# CLANG_CLANG_LIB - Libclang C library
#
# CLANG_CLANGFRONTEND_LIB - Clang Frontend (C++) Library
# CLANG_CLANGDRIVER_LIB - Clang Driver (C++) Library
# ...
#
# CLANG_LIBS - All the Clang C++ libraries
#
# Uses the same include and library paths detected by FindLLVM.cmake
#
# See https://clang.llvm.org/docs/InternalsManual.html for full list of libraries

#=============================================================================
# Copyright 2014-2015 Kevin Funk <[email protected]>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.

#=============================================================================

set(KNOWN_VERSIONS 16)

foreach(version ${KNOWN_VERSIONS})
if(DEFINED Clang_FIND_VERSION AND Clang_FIND_VERSION VERSION_EQUAL version)
find_package(LLVM ${version} PATHS ${LLVM_ROOT})
else()
find_package(LLVM PATHS ${LLVM_ROOT})
endif()
endforeach()

if (${Clang_FIND_REQUIRED})
if(NOT DEFINED LLVM_FOUND)
message(SEND_ERROR "Could not find LLVM (or Clang for that matter)")
else()
message("Found LLVM version ${LLVM_VERSION}")
endif()
endif()

set(CLANG_FOUND FALSE)

if(LLVM_FOUND AND LLVM_LIBRARY_DIRS)
message("Searching for clang libraries...")
macro(FIND_AND_ADD_CLANG_LIB _libname_)
# message("Searching for ${LLVM_LIBRARY_DIRS}/lib${_libname_}-${Clang_FIND_VERSION}.so.1")
string(TOUPPER ${_libname_} _prettylibname_)
find_library(CLANG_${_prettylibname_}_LIB
NAMES
${_libname_}-${Clang_FIND_VERSION}.so.1 lib${_libname_}-${Clang_FIND_VERSION}.so.1
${_libname_}-${Clang_FIND_VERSION} lib${_libname_}-${Clang_FIND_VERSION}
${_libname_}.so.1 lib${_libname_}.so.1
${_libname_} lib${_libname_}
HINTS
${LLVM_LIBRARY_DIRS} ${ARGN})
if(CLANG_${_prettylibname_}_LIB)
message("Found ${CLANG_${_prettylibname_}_LIB}")
set(CLANG_LIBS ${CLANG_LIBS} ${CLANG_${_prettylibname_}_LIB})
endif()
endmacro(FIND_AND_ADD_CLANG_LIB)

FIND_AND_ADD_CLANG_LIB(clangFrontend)

# note: On Windows there's 'libclang.dll' instead of 'clang.dll' -> search for 'libclang', too
FIND_AND_ADD_CLANG_LIB(clang NAMES clang libclang clang-${Clang_FIND_VERSION} libclang-${Clang_FIND_VERSION}) # LibClang: high-level C interface

FIND_AND_ADD_CLANG_LIB(clangDriver)
FIND_AND_ADD_CLANG_LIB(clangCodeGen)
FIND_AND_ADD_CLANG_LIB(clangSema)
FIND_AND_ADD_CLANG_LIB(clangChecker)
FIND_AND_ADD_CLANG_LIB(clangAnalysis)
FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend)
FIND_AND_ADD_CLANG_LIB(clangRewrite)
FIND_AND_ADD_CLANG_LIB(clangAST)
FIND_AND_ADD_CLANG_LIB(clangParse)
FIND_AND_ADD_CLANG_LIB(clangLex)
FIND_AND_ADD_CLANG_LIB(clangBasic)
FIND_AND_ADD_CLANG_LIB(clangARCMigrate)
FIND_AND_ADD_CLANG_LIB(clangEdit)
FIND_AND_ADD_CLANG_LIB(clangFrontendTool)
FIND_AND_ADD_CLANG_LIB(clangSerialization)
FIND_AND_ADD_CLANG_LIB(clangTooling)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCheckers)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCore)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerFrontend)
FIND_AND_ADD_CLANG_LIB(clangRewriteCore)
endif()

if(CLANG_LIBS OR CLANG_CLANG_LIB)
set(CLANG_FOUND TRUE)
else()
message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIRS}")
endif()

if(CLANG_FOUND)
set(CLANG_LIBRARY_DIRS ${LLVM_LIBRARY_DIRS})
set(CLANG_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
set(CLANG_VERSION ${LLVM_VERSION})

# svn version of clang has a svn suffix "8.0.0svn" but installs the header in "8.0.0", without the suffix
string(REPLACE "svn" "" CLANG_VERSION_CLEAN "${CLANG_VERSION}")
# dito for git
string(REPLACE "git" "" CLANG_VERSION_CLEAN "${CLANG_VERSION}")

find_path(CLANG_BUILTIN_DIR
# cpuid.h because it is defined in ClangSupport constructor as valid clang builtin dir indicator
NAMES "cpuid.h"
PATHS "${CLANG_LIBRARY_DIRS}"
"${CLANG_INCLUDE_DIRS}"
PATH_SUFFIXES "clang/${CLANG_VERSION}/include"
"../../../clang/${CLANG_VERSION}/include"
"clang/${CLANG_VERSION_CLEAN}/include"
"../../../clang/${CLANG_VERSION_CLEAN}/include"
NO_DEFAULT_PATH
)

if (NOT CLANG_BUILTIN_DIR)
message(FATAL_ERROR "Could not find Clang builtin directory")
endif()
get_filename_component(CLANG_BUILTIN_DIR ${CLANG_BUILTIN_DIR} ABSOLUTE)

# check whether llvm-config comes from an install prefix
execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --src-root
OUTPUT_VARIABLE _llvmSourceRoot
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(FIND "${LLVM_INCLUDE_DIRS}" "${_llvmSourceRoot}" _llvmIsInstalled)
if (NOT _llvmIsInstalled)
message(STATUS "Detected that llvm-config comes from a build-tree, adding more include directories for Clang")
list(APPEND CLANG_INCLUDE_DIRS
"${LLVM_INSTALL_PREFIX}/tools/clang/include" # build dir
)

# check whether the source is from llvm-project.git (currently recommended way to clone the LLVM projects)
# contains all LLVM projects in the top-level directory
get_filename_component(_llvmProjectClangIncludeDir ${_llvmSourceRoot}/../clang/include REALPATH)
if (EXISTS ${_llvmProjectClangIncludeDir})
message(STATUS " Note: llvm-project.git structure detected, using different include path pointing into source dir")
list(APPEND CLANG_INCLUDE_DIRS "${_llvmProjectClangIncludeDir}") # source dir
else()
list(APPEND CLANG_INCLUDE_DIRS "${_llvmSourceRoot}/tools/clang/include") # source dir
endif()
endif()

# if the user specified LLVM_ROOT, use that and fail otherwise
if (LLVM_ROOT)
find_program(CLANG_EXECUTABLE NAMES clang HINTS ${LLVM_ROOT}/bin DOC "clang executable" NO_DEFAULT_PATH)
elseif (NOT CLANG_EXECUTABLE)
# find clang, prefer the one with a version suffix, e.g. clang-3.5
# note: FreeBSD installs clang as clang35 and so on
# note: on some distributions, only 'clang' is shipped, so let's always try to fallback on that
string(REPLACE "." "" Clang_FIND_VERSION_CONCAT ${Clang_FIND_VERSION})
find_program(CLANG_EXECUTABLE NAMES clang-${Clang_FIND_VERSION} clang${Clang_FIND_VERSION_CONCAT} clang DOC "clang executable")
endif()

message(STATUS "Found Clang (LLVM version: ${CLANG_VERSION})")
message(STATUS " Include dirs: ${CLANG_INCLUDE_DIRS}")
message(STATUS " Clang libraries: ${CLANG_LIBS}")
message(STATUS " Libclang C library: ${CLANG_CLANG_LIB}")
message(STATUS " Builtin include dir: ${CLANG_BUILTIN_DIR}")
message(STATUS " Clang executable: ${CLANG_EXECUTABLE}")
else()
if(Clang_FIND_REQUIRED)
message(FATAL_ERROR "Could NOT find Clang")
endif()
endif()
Loading

0 comments on commit 7735281

Please sign in to comment.