diff --git a/modules/gmake2/gmake2_cpp.lua b/modules/gmake2/gmake2_cpp.lua index 63b3d331b..c22aaa899 100644 --- a/modules/gmake2/gmake2_cpp.lua +++ b/modules/gmake2/gmake2_cpp.lua @@ -60,7 +60,13 @@ fileExtension { ".cc", ".cpp", ".cxx", ".mm" } buildoutputs { "$(OBJDIR)/%{file.objname}.o" } buildmessage '$(notdir $<)' - buildcommands {'$(CXX) %{premake.modules.gmake2.cpp.fileFlags(cfg, file)} $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"'} + buildcommands { + -- code checking must be before the build, so that if the code checking + -- fails, the build is not completed, and hence, a re-run will trigger + -- the code check again. + '$(CLANG_TIDY) "$<" -- %{premake.modules.gmake2.cpp.fileFlags(cfg, file)} $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"', + '$(CXX) %{premake.modules.gmake2.cpp.fileFlags(cfg, file)} $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"' + } rule 'cc' fileExtension {".c", ".s", ".m"} @@ -326,6 +332,7 @@ cpp.cppFlags, cpp.cFlags, cpp.cxxFlags, + cpp.clangtidy, cpp.resFlags, cpp.libs, cpp.ldDeps, @@ -425,6 +432,13 @@ p.outln('ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS)' .. flags) end + function cpp.clangtidy(cfg, toolset) + if cfg.clangtidy ~= nil then + p.outln('CLANG_TIDY = clang-tidy') + else + p.outln('CLANG_TIDY = \\#') + end + end function cpp.resFlags(cfg, toolset) local resflags = table.join(toolset.getdefines(cfg.resdefines), toolset.getincludedirs(cfg, cfg.resincludedirs), cfg.resoptions) diff --git a/modules/gmake2/tests/_tests.lua b/modules/gmake2/tests/_tests.lua index 5b866d1ed..224671c56 100644 --- a/modules/gmake2/tests/_tests.lua +++ b/modules/gmake2/tests/_tests.lua @@ -3,6 +3,7 @@ require ("gmake2") return { "test_gmake2_buildcmds.lua", "test_gmake2_clang.lua", + "test_gmake2_clangtidy.lua", "test_gmake2_file_rules.lua", "test_gmake2_flags.lua", "test_gmake2_includes.lua", diff --git a/modules/gmake2/tests/test_gmake2_clangtidy.lua b/modules/gmake2/tests/test_gmake2_clangtidy.lua new file mode 100644 index 000000000..7e080e45d --- /dev/null +++ b/modules/gmake2/tests/test_gmake2_clangtidy.lua @@ -0,0 +1,45 @@ +-- +-- test_gmake2_clangtidy.lua +-- Test ClangTidy support in Makefiles. +-- + +local suite = test.declare("gmake2_clangtidy") +local gmake2 = premake.modules.gmake2 + +local wks, prj, cfg + +function suite.setup() + wks = workspace("MyWorkspace") + configurations { "Debug", "Release" } + targetname "blink" + kind "StaticLib" + language "C++" + prj = test.createProject(wks) +end + +local function prepare() + wks = test.getWorkspace(wks) + prj = test.getproject(wks, 1) + cfg = test.getconfig(prj, "Debug") + gmake2.cpp.clangtidy(cfg, toolset) + files { "src/hello.cpp", "src/hello2.c" } + +end + +function suite.clangtidyOn() + clangtidy "On" + + prepare() + + test.capture [[ +CLANG_TIDY = clang-tidy + ]] +end + +function suite.clangtidyOff() + prepare() + + test.capture [[ +CLANG_TIDY = \# + ]] +end diff --git a/modules/gmake2/tests/test_gmake2_file_rules.lua b/modules/gmake2/tests/test_gmake2_file_rules.lua index 4e61ce975..800ccf803 100644 --- a/modules/gmake2/tests/test_gmake2_file_rules.lua +++ b/modules/gmake2/tests/test_gmake2_file_rules.lua @@ -94,9 +94,11 @@ $(OBJDIR)/hello.o: src/greetings/hello.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/hello1.o: src/hello.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" ]] @@ -119,6 +121,7 @@ $(OBJDIR)/hello.o: src/hello.c $(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/test.o: src/test.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" ]] @@ -139,6 +142,7 @@ $(OBJDIR)/test.o: src/test.cpp $(OBJDIR)/hello.o: src/hello.c @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/test.o: src/test.c @echo "$(notdir $<)" @@ -166,6 +170,7 @@ $(OBJDIR)/test.o: src/test.c ifeq ($(config),debug) $(OBJDIR)/hello.o: src/hello.c @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" else ifeq ($(config),release) diff --git a/modules/gmake2/tests/test_gmake2_pch.lua b/modules/gmake2/tests/test_gmake2_pch.lua index 395d2bbfe..05b441a5d 100644 --- a/modules/gmake2/tests/test_gmake2_pch.lua +++ b/modules/gmake2/tests/test_gmake2_pch.lua @@ -70,9 +70,11 @@ $(OBJDIR)/a.o: a.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/b.o: b.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" ]] end @@ -194,9 +196,11 @@ PCH = ../../../../src/host/premake.h $(OBJDIR)/a.o: a.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- -include $(PCH_PLACEHOLDER) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) -include $(PCH_PLACEHOLDER) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/b.o: b.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- -include $(PCH_PLACEHOLDER) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) -include $(PCH_PLACEHOLDER) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" ]] end @@ -216,9 +220,11 @@ $(OBJDIR)/b.o: b.cpp $(OBJDIR)/a.o: a.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/b.o: b.cpp @echo "$(notdir $<)" + $(SILENT) $(CLANG_TIDY) "$<" -- -include $(PCH_PLACEHOLDER) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(SILENT) $(CXX) -include $(PCH_PLACEHOLDER) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" ]] end diff --git a/website/docs/clangtidy.md b/website/docs/clangtidy.md index 6af12a442..79a2d7f19 100644 --- a/website/docs/clangtidy.md +++ b/website/docs/clangtidy.md @@ -1,6 +1,6 @@ Enables clang-tidy code analysis for Visual Studio. -The `clangtidy` option enables running clang-tidy code analysis in Visual Studio projects. +The `clangtidy` option enables running clang-tidy code analysis in Visual Studio and gmake2 projects. ```lua clangtidy("value") @@ -19,7 +19,7 @@ The `config` scope. ### Availability ### -Premake 5.0.0 beta 3 or later for Visual Studio 2019 and later. +Premake 5.0.0 beta 3 or later. Implemented for Visual Studio 2019 and later, and gmake2. ### See Also ###