try to autodetect flags needed to build Clang plugins

Instead of having a lot of it hardcoded, which brings problems like:
- Clang-to-be-10 has switched to -std=c++14, so our hardcoded c++11
  makes the build fail
- I cannot compile with my openSUSE-shipped clang, because it ships
  only libclang-cpp and not the other libClangSomething libs
The possibility to explicitly set the necessary variables is still there.

Change-Id: I58d401d4584fa064f1c1351a8a06ff4e29643063
Reviewed-on: https://gerrit.libreoffice.org/80300
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
This commit is contained in:
Luboš Luňák
2019-10-06 15:01:07 +02:00
parent b1c14c30ba
commit ad5cbcf6ba
5 changed files with 95 additions and 43 deletions

View File

@@ -23,14 +23,14 @@ CLANGCXXFLAGS=/nologo /D_HAS_EXCEPTIONS=0 /wd4141 /wd4577 /EHs-c- /GR-
ifeq ($(CLANGDEBUG),)
CLANGCXXFLAGS+=/O2 /Oi
else
CLANGCXXFLAGS+=/DEBUG
CLANGCXXFLAGS+=/DEBUG /Od
endif
else # WNT
CLANGCXXFLAGS=-Wall -Wextra -Wundef
ifeq ($(CLANGDEBUG),)
CLANGCXXFLAGS+=-O2
else
CLANGCXXFLAGS+=-g
CLANGCXXFLAGS+=-g -O0 -UNDEBUG
endif
endif
@@ -61,20 +61,13 @@ CLANG_EXE_EXT =
endif
# Clang headers require these.
CLANGDEFS=-D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
ifneq ($(OS),WNT)
CLANGDEFS += -fno-rtti
endif
CLANGDEFS:=$(COMPILER_PLUGINS_CXXFLAGS)
# All include locations needed (using -isystem silences various warnings when
# including those files):
ifeq ($(OS),WNT)
CLANGINCLUDES=-I$(CLANGDIR)/include
else
CLANGINCLUDES=$(if $(filter /usr,$(CLANGDIR)),,-isystem $(CLANGDIR)/include)
ifneq ($(OS),WNT)
CLANGDEFS:=$(filter-out -isystem/usr/include,$(foreach opt,$(CLANGDEFS),$(patsubst -I%,-isystem%,$(opt))))
endif
LLVMCONFIG=$(CLANGDIR)/bin/llvm-config
# Clang/LLVM libraries are intentionally not linked in, they are usually built as static libraries, which means the resulting
# plugin would be big (even though the clang binary already includes it all) and it'd be necessary to explicitly specify
# also all the dependency libraries.
@@ -166,7 +159,7 @@ ifeq ($(OS),WNT)
define clangbuildsrc
$(2): $(1) $(SRCDIR)/compilerplugins/Makefile-clang.mk $(CLANGOUTDIR)/clang-timestamp
$$(call gb_Output_announce,$(subst $(SRCDIR)/,,$(subst $(BUILDDIR)/,,$(1))),$(true),CXX,3)
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGCXXFLAGS) $(CLANGWERROR) $(CLANGDEFS) \
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGDEFS) $(CLANGCXXFLAGS) $(CLANGWERROR) \
$(CLANGINCLUDES) /I$(BUILDDIR)/config_host /I$(CLANGINDIR) $(1) /MD \
/c /Fo: $(2)
@@ -182,7 +175,7 @@ else
define clangbuildsrc
$(2): $(1) $(SRCDIR)/compilerplugins/Makefile-clang.mk $(CLANGOUTDIR)/clang-timestamp
$$(call gb_Output_announce,$(subst $(SRCDIR)/,,$(subst $(BUILDDIR)/,,$(1))),$(true),CXX,3)
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGCXXFLAGS) $(CLANGWERROR) $(CLANGDEFS) \
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGDEFS) $(CLANGCXXFLAGS) $(CLANGWERROR) \
$(CLANGINCLUDES) -I$(BUILDDIR)/config_host -I$(CLANGINDIR) $(1) \
-fPIC -c -o $(2) -MMD -MT $(2) -MP -MF $(3)
@@ -231,38 +224,37 @@ $(CLANGOUTDIR)/sharedvisitor/sharedvisitor.cxx: $(SHARED_SOURCE_INFOS) $(CLANGOU
$(QUIET)$(ICECREAM_RUN) $(CLANGOUTDIR)/sharedvisitor/generator$(CLANG_EXE_EXT) \
$(SHARED_SOURCE_INFOS) > $@
CLANGTOOLLIBS = -lclangTooling -lclangDriver -lclangFrontend -lclangParse -lclangSema -lclangEdit -lclangAnalysis \
-lclangAST -lclangLex -lclangSerialization -lclangBasic $(shell $(LLVMCONFIG) --ldflags --libs --system-libs)
# Path to the clang system headers (no idea if there's a better way to get it).
CLANGTOOLDEFS = -DCLANGSYSINCLUDE=$(shell $(LLVMCONFIG) --libdir)/clang/$(shell $(LLVMCONFIG) --version | sed 's/svn//')/include
# -std=c++11 is in line with the default value for COMPILER_PLUGINS_CXX in configure.ac:
CLANGSTDOPTION := $(or $(filter -std=%,$(COMPILER_PLUGINS_CXX)),-std=c++11)
CLANGTOOLDEFS += -DSTDOPTION=\"$(CLANGSTDOPTION)\"
# Flags used internally in analyzer.
# Older versions of Clang have a problem to find their own internal headers, so add it.
# Also filter out the c++ library, it's not necessary to be specific about it in this case
# and it can also cause trouble with finding the proper headers.
CLANGTOOLDEFS = $(filter-out -stdlib=%,$(CLANGDEFS) -I$(CLANGSYSINCLUDE))
CLANGTOOLDEFS += -w
ifneq ($(filter-out MACOSX WNT,$(OS)),)
ifneq ($(CLANGDIR),/usr)
# Help the generator find Clang shared libs, if Clang is built so and installed in a non-standard prefix.
CLANGTOOLLIBS += -Wl,--rpath,$(shell $(LLVMCONFIG) --libdir)
CLANGTOOLLIBS += -Wl,--rpath,$(CLANGLIBDIR)
endif
endif
$(CLANGOUTDIR)/sharedvisitor/analyzer$(CLANG_EXE_EXT): $(CLANGINDIR)/sharedvisitor/analyzer.cxx \
| $(CLANGOUTDIR)/sharedvisitor
$(call gb_Output_announce,$(subst $(BUILDDIR)/,,$@),$(true),GEN,1)
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGCXXFLAGS) $(CLANGWERROR) $(CLANGDEFS) $(CLANGTOOLDEFS) $(CLANGINCLUDES) \
-DCLANGDIR=$(CLANGDIR) -I$(BUILDDIR)/config_host \
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGDEFS) $(CLANGCXXFLAGS) $(CLANGWERROR) $(CLANGINCLUDES) \
-I$(BUILDDIR)/config_host -DCLANGFLAGS="$(CLANGTOOLDEFS)" \
-DLO_CLANG_USE_ANALYZER_PCH=$(LO_CLANG_USE_ANALYZER_PCH) \
-c $< -o $(CLANGOUTDIR)/sharedvisitor/analyzer.o -MMD -MT $@ -MP \
-MF $(CLANGOUTDIR)/sharedvisitor/analyzer.d
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGCXXFLAGS) $(CLANGOUTDIR)/sharedvisitor/analyzer.o \
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGDEFS) $(CLANGCXXFLAGS) $(CLANGOUTDIR)/sharedvisitor/analyzer.o \
-o $@ $(CLANGTOOLLIBS)
$(CLANGOUTDIR)/sharedvisitor/generator$(CLANG_EXE_EXT): $(CLANGINDIR)/sharedvisitor/generator.cxx \
| $(CLANGOUTDIR)/sharedvisitor
$(call gb_Output_announce,$(subst $(BUILDDIR)/,,$@),$(true),GEN,1)
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGCXXFLAGS) $(CLANGWERROR) \
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGDEFS) $(CLANGCXXFLAGS) $(CLANGWERROR) \
-c $< -o $(CLANGOUTDIR)/sharedvisitor/generator.o -MMD -MT $@ -MP \
-MF $(CLANGOUTDIR)/sharedvisitor/generator.d
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGCXXFLAGS) $(CLANGOUTDIR)/sharedvisitor/generator.o \
$(QUIET)$(COMPILER_PLUGINS_CXX) $(CLANGDEFS) $(CLANGCXXFLAGS) $(CLANGOUTDIR)/sharedvisitor/generator.o \
-o $@
$(CLANGOUTDIR)/sharedvisitor/analyzer$(CLANG_EXE_EXT): $(SRCDIR)/compilerplugins/Makefile-clang.mk $(CLANGOUTDIR)/clang-timestamp
@@ -302,8 +294,7 @@ endif
ifdef LO_CLANG_USE_ANALYZER_PCH
# these are from the invocation in analyzer.cxx
LO_CLANG_ANALYZER_PCH_CXXFLAGS := -I$(BUILDDIR)/config_host -I$(CLANGDIR)/include $(CLANGTOOLDEFS) $(CLANGSTDOPTION) \
-D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
LO_CLANG_ANALYZER_PCH_CXXFLAGS := -I$(BUILDDIR)/config_host $(CLANGTOOLDEFS)
$(CLANGOUTDIR)/sharedvisitor/clang.pch: $(CLANGINDIR)/sharedvisitor/precompiled_clang.hxx \
$(SRCDIR)/compilerplugins/Makefile-clang.mk $(CLANGOUTDIR)/clang-timestamp \

View File

@@ -12,6 +12,7 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/StringExtras.h"
#include <cstddef>
#include <cstring>
@@ -27,6 +28,7 @@
using namespace std;
using namespace clang;
using namespace llvm;
using namespace loplugin;
@@ -241,16 +243,13 @@ int main(int argc, char** argv)
}
#define STRINGIFY2(a) #a
#define STRINGIFY(a) STRINGIFY2(a)
SmallVector< StringRef, 20 > clangflags;
SplitString( STRINGIFY(CLANGFLAGS), clangflags );
args.insert( args.end(), clangflags.begin(), clangflags.end());
args.insert(
args.end(),
{ // These must match LO_CLANG_ANALYZER_PCH_CXXFLAGS in Makefile-clang.mk .
"-I" BUILDDIR "/config_host", // plugin sources use e.g. config_global.h
"-I" STRINGIFY(CLANGDIR) "/include", // clang's headers
"-I" STRINGIFY(CLANGSYSINCLUDE), // clang system headers
STDOPTION,
"-D__STDC_CONSTANT_MACROS", // Clang headers require these.
"-D__STDC_FORMAT_MACROS",
"-D__STDC_LIMIT_MACROS"
"-I" BUILDDIR "/config_host" // plugin sources use e.g. config_global.h
#ifdef LO_CLANG_USE_ANALYZER_PCH
,
"-include-pch", // use PCH with Clang headers to speed up parsing/analysing
@@ -264,9 +263,9 @@ int main(int argc, char** argv)
continue;
foundSomething = false;
#if CLANG_VERSION >= 100000
if( !clang::tooling::runToolOnCodeWithArgs( std::unique_ptr<FindNamedClassAction>(new FindNamedClassAction), contents, args, argv[ i ] ))
if( !tooling::runToolOnCodeWithArgs( std::unique_ptr<FindNamedClassAction>(new FindNamedClassAction), contents, args, argv[ i ] ))
#else
if( !clang::tooling::runToolOnCodeWithArgs( new FindNamedClassAction, contents, args, argv[ i ] ))
if( !tooling::runToolOnCodeWithArgs( new FindNamedClassAction, contents, args, argv[ i ] ))
#endif
{
cerr << "Failed to analyze: " << argv[ i ] << endl;

View File

@@ -124,7 +124,7 @@ void generate()
"} // namespace loplugin\n"
"\n"
"#endif // LO_CLANG_SHARED_PLUGINS\n";
};
}
void generateVisitor( PluginType type )
{

View File

@@ -65,6 +65,8 @@ export CDR_LIBS=$(gb_SPACE)@CDR_LIBS@
@x_CFLAGS@ export CFLAGS=@CFLAGS@
export CLANGDIR=@CLANGDIR@
export CLANGLIBDIR=@CLANGLIBDIR@
export CLANGSYSINCLUDE=@CLANGSYSINCLUDE@
export CLANGTOOLLIBS=@CLANGTOOLLIBS@
export CLUCENE_CFLAGS=$(gb_SPACE)@CLUCENE_CFLAGS@
export CLUCENE_LIBS=$(gb_SPACE)@CLUCENE_LIBS@
export LIBCMIS_CFLAGS=$(gb_SPACE)@LIBCMIS_CFLAGS@
@@ -75,6 +77,7 @@ export COMMONS_LOGGING_VERSION=@COMMONS_LOGGING_VERSION@
export COMPATH=@COMPATH@
export COMPILER_PLUGINS=@COMPILER_PLUGINS@
export COMPILER_PLUGINS_CXX=@COMPILER_PLUGINS_CXX@
export COMPILER_PLUGINS_CXXFLAGS=@COMPILER_PLUGINS_CXXFLAGS@
export COMPILER_PLUGINS_CXX_LINKFLAGS=@COMPILER_PLUGINS_CXX_LINKFLAGS@
export COMPILER_PLUGINS_DEBUG=@COMPILER_PLUGINS_DEBUG@
export COMPILER_PLUGINS_TOOLING_ARGS=@COMPILER_PLUGINS_TOOLING_ARGS@

View File

@@ -7012,15 +7012,30 @@ if test "$COM_IS_CLANG" = "TRUE"; then
if test -z "$CLANGDIR"; then
CLANGDIR=$(dirname $(dirname $($CXX -print-prog-name=$(basename $CXX))))
fi
# Assume Clang is self-built, but allow overriding COMPILER_PLUGINS_CXX to the compiler Clang was built with.
if test -z "$COMPILER_PLUGINS_CXX"; then
COMPILER_PLUGINS_CXX=$CXX
fi
AC_PATH_PROG(LLVM_CONFIG, llvm-config,[],$CLANGDIR/bin $PATH)
if test -n "$LLVM_CONFIG"; then
COMPILER_PLUGINS_CXXFLAGS=$($LLVM_CONFIG --cxxflags)
COMPILER_PLUGINS_CXX_LINKFLAGS=$($LLVM_CONFIG --ldflags --libs --system-libs | tr '\n' ' ')
if test -z "$CLANGLIBDIR"; then
CLANGLIBDIR=$($LLVM_CONFIG --libdir)
fi
fi
AC_MSG_NOTICE([compiler plugins compile flags: $COMPILER_PLUGINS_CXXFLAGS])
AC_LANG_PUSH([C++])
save_CPPFLAGS=$CPPFLAGS
save_CXX=$CXX
save_CXXCPP=$CXXCPP
# compiler plugins must be built with "native" compiler that was used to build Clang itself:
: "${COMPILER_PLUGINS_CXX=g++ -std=c++11}"
save_CPPFLAGS=$CPPFLAGS
save_CXXFLAGS=$CXXFLAGS
save_LDFLAGS=$LDFLAGS
save_LIBS=$LIBS
CXX=$COMPILER_PLUGINS_CXX
CXXCPP="$COMPILER_PLUGINS_CXX -E"
CPPFLAGS="$CPPFLAGS -I$CLANGDIR/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS"
CPPFLAGS="$COMPILER_PLUGINS_CXXFLAGS"
CXXFLAGS="$COMPILER_PLUGINS_CXXFLAGS"
AC_CHECK_HEADER(clang/AST/RecursiveASTVisitor.h,
[COMPILER_PLUGINS=TRUE],
[
@@ -7031,9 +7046,50 @@ if test "$COM_IS_CLANG" = "TRUE"; then
add_warning "Cannot find Clang headers to build compiler plugins, plugins disabled."
fi
])
if test -n "$COMPILER_PLUGINS"; then
LDFLAGS=""
AC_MSG_CHECKING([for clang libraries to use])
if test -z "$CLANGTOOLLIBS"; then
LIBS="-lclangTooling -lclangDriver -lclangFrontend -lclangParse -lclangSema -lclangEdit \
-lclangAnalysis -lclangAST -lclangLex -lclangSerialization -lclangBasic $COMPILER_PLUGINS_CXX_LINKFLAGS"
AC_LINK_IFELSE([
AC_LANG_PROGRAM([[#include "clang/Tooling/Tooling.h"]],
[[ clang::tooling::runToolOnCode( nullptr, "some code" ); ]])
],[CLANGTOOLLIBS="$LIBS"],[])
fi
if test -z "$CLANGTOOLLIBS"; then
LIBS="-lclang-cpp $COMPILER_PLUGINS_CXX_LINKFLAGS"
AC_LINK_IFELSE([
AC_LANG_PROGRAM([[#include "clang/Tooling/Tooling.h"]],
[[ clang::tooling::runToolOnCode( nullptr, "some code" ); ]])
],[CLANGTOOLLIBS="$LIBS"],[])
fi
AC_MSG_RESULT([$CLANGTOOLLIBS])
if test -z "$CLANGTOOLLIBS"; then
if test "$compiler_plugins" = "yes"; then
cat config.log
AC_MSG_ERROR([Cannot find Clang libraries to build compiler plugins.])
else
AC_MSG_WARN([Cannot find Clang libraries to build compiler plugins, plugins disabled])
add_warning "Cannot find Clang libraries to build compiler plugins, plugins disabled."
fi
COMPILER_PLUGINS=
fi
if test -n "$COMPILER_PLUGINS"; then
if test -z "$CLANGSYSINCLUDE"; then
if test -n "$LLVM_CONFIG"; then
# Path to the clang system headers (no idea if there's a better way to get it).
CLANGSYSINCLUDE=$($LLVM_CONFIG --libdir)/clang/$($LLVM_CONFIG --version | sed 's/svn//')/include
fi
fi
fi
fi
CXX=$save_CXX
CXXCPP=$save_CXXCPP
CPPFLAGS=$save_CPPFLAGS
CXXFLAGS=$save_CXXFLAGS
LDFLAGS=$save_LDFLAGS
LIBS="$save_LIBS"
AC_LANG_POP([C++])
fi
else
@@ -7043,11 +7099,14 @@ else
fi
AC_SUBST(COMPILER_PLUGINS)
AC_SUBST(COMPILER_PLUGINS_CXX)
AC_SUBST(COMPILER_PLUGINS_CXXFLAGS)
AC_SUBST(COMPILER_PLUGINS_CXX_LINKFLAGS)
AC_SUBST(COMPILER_PLUGINS_DEBUG)
AC_SUBST(COMPILER_PLUGINS_TOOLING_ARGS)
AC_SUBST(CLANGDIR)
AC_SUBST(CLANGLIBDIR)
AC_SUBST(CLANGTOOLLIBS)
AC_SUBST(CLANGSYSINCLUDE)
# Plugin to help linker.
# Add something like LD_PLUGIN=/usr/lib64/LLVMgold.so to your autogen.input.