From f27aa8df3d71f6f30ec0cefe030f68a3537baa13 Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Fri, 2 Aug 2019 14:04:11 +0200 Subject: [PATCH] Implement check_{function,symbol}_exists Required by and used in https://github.com/measurement-kit/libndt/pull/108 --- cmake/cmake.go | 6 ++++++ cmake/cmakefile/cmakefile.go | 27 +++++++++++++++++++++++++++ pkginfo/pkginfo.go | 27 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/cmake/cmake.go b/cmake/cmake.go index a41afdf..4b96609 100644 --- a/cmake/cmake.go +++ b/cmake/cmake.go @@ -50,6 +50,12 @@ func sortedTestInfo(m map[string]pkginfo.TestInfo) []string { func Generate(pkginfo *pkginfo.PkgInfo) { cmake := cmakefile.Open(pkginfo.Name) defer cmake.Close() + for _, funcheck := range pkginfo.FunctionChecks { + cmake.CheckFunctionExists(funcheck.Name, funcheck.Define) + } + for _, symcheck := range pkginfo.SymbolChecks { + cmake.CheckSymbolExists(symcheck.Name, symcheck.Header, symcheck.Define) + } for _, depname := range pkginfo.Dependencies { handler, ok := deps.All[depname] if !ok { diff --git a/cmake/cmakefile/cmakefile.go b/cmake/cmakefile/cmakefile.go index 46548fe..3a5f70c 100644 --- a/cmake/cmakefile/cmakefile.go +++ b/cmake/cmakefile/cmakefile.go @@ -104,6 +104,9 @@ func Open(name string) *CMakeFile { cmake.WriteLine(` set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")`) cmake.WriteLine(` set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L/usr/local/lib")`) cmake.WriteLine(`endif()`) + cmake.writeEmptyLine() + cmake.WriteLine(`include(CheckFunctionExists)`) + cmake.WriteLine(`include(CheckSymbolExists)`) return cmake } @@ -154,6 +157,30 @@ func (cmake *CMakeFile) untar(filename, destdir string) { cmake.unzip(filename, destdir) } +// CheckFunctionExists checks whether |name| is a function and +// defines the |define| preprocessor macro in such case. +func (cmake *CMakeFile) CheckFunctionExists(name, define string) { + cmake.WriteLine(fmt.Sprintf("check_function_exists(%s %s)", name, define)) + cmake.WriteLine(fmt.Sprintf("if(${%s})", define)) + cmake.WithIndent(" ", func() { + cmake.WriteLine(fmt.Sprintf("add_definitions(-D%s)", define)) + }) + cmake.WriteLine("endif()") +} + +// CheckSymbolExists checks whether |name| is a symbol in |header| and +// defines the |define| preprocessor macro in such case. +func (cmake *CMakeFile) CheckSymbolExists(name, header, define string) { + cmake.WriteLine(fmt.Sprintf( + "check_symbol_exists(%s %s %s)", name, header, define, + )) + cmake.WriteLine(fmt.Sprintf("if(${%s})", define)) + cmake.WithIndent(" ", func() { + cmake.WriteLine(fmt.Sprintf("add_definitions(-D%s)", define)) + }) + cmake.WriteLine("endif()") +} + // AddRequiredDefinition adds |definition| to the macro definitions func (cmake *CMakeFile) AddRequiredDefinition(definition string) { cmake.WriteLine(fmt.Sprintf( diff --git a/pkginfo/pkginfo.go b/pkginfo/pkginfo.go index 9cc004d..d18b3a6 100644 --- a/pkginfo/pkginfo.go +++ b/pkginfo/pkginfo.go @@ -59,11 +59,38 @@ type TestInfo struct { Command string } +// FunctionCheck adds a check for a specific function +type FunctionCheck struct { + // Name is the function name + Name string + + // Define is the define to add to the build if function exists + Define string +} + +// SymbolCheck adds a check for a specific symbol +type SymbolCheck struct { + // Name is the symbol name + Name string + + // Header is the header to include for checking for the symbol + Header string + + // Define is the define to add to the build if symbol exists + Define string +} + // PkgInfo contains information on a package type PkgInfo struct { // Name is the name of the package Name string + // FunctionChecks contains all the checks for functions + FunctionChecks []FunctionCheck `yaml:"function_checks"` + + // SymbolChecks contains all the checks for symbols + SymbolChecks []SymbolCheck `yaml:"symbol_checks"` + // Docker is the docker container to use for running tests Docker string