premake/src/actions/make/make_cpp.lua

184 lines
6.3 KiB
Lua
Raw Normal View History

--
-- make_cpp.lua
-- Generate a C/C++ project makefile.
-- Copyright (c) 2002-2009 Jason Perkins and the Premake project
--
function premake.make_cpp(prj)
-- create a shortcut to the compiler interface
local cc = premake[_OPTIONS.cc]
-- write a quick header
io.printf('# %s project makefile autogenerated by Premake', premake.actions[_ACTION].shortname)
-- set up the environment
io.printf('ifndef config')
io.printf(' config=%s', _MAKE.esc(prj.configurations[1]:lower()))
io.printf('endif')
io.printf('')
io.printf('ifndef verbose')
io.printf(' SILENT = @')
io.printf('endif')
io.printf('')
-- list the configurations
for cfg in premake.eachconfig(prj) do
io.printf('ifeq ($(config),%s)', _MAKE.esc(cfg.name:lower()))
io.printf(' TARGETDIR = %s', _MAKE.esc(cfg.buildtarget.directory))
io.printf(' TARGET = $(TARGETDIR)/%s', _MAKE.esc(cfg.buildtarget.name))
io.printf(' OBJDIR = %s', _MAKE.esc(cfg.objectsdir))
io.printf(' DEFINES += %s', table.concat(cc.getdefines(cfg.defines), " "))
io.printf(' INCLUDES += %s', table.concat(cc.getincludedirs(cfg.includedirs), " "))
io.printf(' CPPFLAGS += %s $(DEFINES) $(INCLUDES)', cc.getcppflags(cfg))
io.printf(' CFLAGS += $(CPPFLAGS) $(ARCH) %s', table.concat(table.join(cc.getcflags(cfg), cfg.buildoptions), " "))
io.printf(' CXXFLAGS += $(CFLAGS) %s', table.concat(cc.getcxxflags(cfg), " "))
io.printf(' LDFLAGS += %s', table.concat(table.join(cc.getldflags(cfg), cc.getlinkflags(cfg), cfg.linkoptions), " "))
io.printf(' RESFLAGS += $(DEFINES) $(INCLUDES) %s', table.concat(table.join(cc.getdefines(cfg.resdefines), cc.getincludedirs(cfg.resincludedirs), cfg.resoptions), " "))
io.printf(' LDDEPS += %s', table.concat(_MAKE.esc(premake.getlinks(cfg, "siblings", "fullpath")), " "))
if cfg.kind == "StaticLib" then
io.printf(' LINKCMD = ar -rcs $(TARGET) $(OBJECTS)')
else
io.printf(' LINKCMD = $(%s) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(ARCH)', iif(cfg.language == "C", "CC", "CXX"))
end
io.printf(' define PREBUILDCMDS')
if #cfg.prebuildcommands > 0 then
io.printf('\t@echo Running pre-build commands')
io.printf('\t%s', table.implode(cfg.prebuildcommands, "", "", "\n\t"))
end
io.printf(' endef')
io.printf(' define PRELINKCMDS')
if #cfg.prelinkcommands > 0 then
io.printf('\t@echo Running pre-link commands')
io.printf('\t%s', table.implode(cfg.prelinkcommands, "", "", "\n\t"))
end
io.printf(' endef')
io.printf(' define POSTBUILDCMDS')
if #cfg.postbuildcommands > 0 then
io.printf('\t@echo Running post-build commands')
io.printf('\t%s', table.implode(cfg.postbuildcommands, "", "", "\n\t"))
end
io.printf(' endef')
io.printf('endif')
io.printf('')
end
-- list intermediate files
io.printf('OBJECTS := \\')
for _, file in ipairs(prj.files) do
if path.iscppfile(file) then
io.printf('\t$(OBJDIR)/%s.o \\', _MAKE.esc(path.getbasename(file)))
end
end
io.printf('')
io.printf('RESOURCES := \\')
for _, file in ipairs(prj.files) do
if path.isresourcefile(file) then
io.printf('\t$(OBJDIR)/%s.res \\', _MAKE.esc(path.getbasename(file)))
end
end
io.printf('')
-- set up support commands like mkdir, rmdir, etc. based on the shell
io.printf('SHELLTYPE := msdos')
io.printf('ifeq (,$(ComSpec)$(COMSPEC))')
io.printf(' SHELLTYPE := posix')
io.printf('endif')
io.printf('ifeq (/bin,$(findstring /bin,$(SHELL)))')
io.printf(' SHELLTYPE := posix')
io.printf('endif')
io.printf('ifeq (posix,$(SHELLTYPE))')
io.printf(' define MKDIR_RULE')
io.printf('\t@echo Creating $@')
io.printf('\t$(SILENT) mkdir -p $@')
io.printf(' endef')
io.printf('else')
io.printf(' define MKDIR_RULE')
io.printf('\t@echo Creating $@')
io.printf('\t$(SILENT) mkdir $(subst /,\\\\,$@)')
io.printf(' endef')
io.printf('endif')
io.printf('')
-- main build rule(s)
io.printf('.PHONY: clean prebuild prelink')
io.printf('')
if os.is("MacOSX") and prj.kind == "WindowedApp" then
io.printf('all: $(TARGETDIR) $(OBJDIR) prebuild $(OBJECTS) $(RESOURCES) prelink $(TARGET) $(dir $(TARGETDIR))PkgInfo $(dir $(TARGETDIR))Info.plist')
else
io.printf('all: $(TARGETDIR) $(OBJDIR) prebuild $(OBJECTS) $(RESOURCES) prelink $(TARGET)')
end
io.printf('')
io.printf('$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)')
io.printf('\t@echo Linking %s', prj.name)
io.printf('\t@$(LINKCMD)')
io.printf('\t$(POSTBUILDCMDS)')
io.printf('')
-- create destination directories
io.printf('$(TARGETDIR):')
io.printf('\t$(MKDIR_RULE)')
io.printf('')
io.printf('$(OBJDIR):')
io.printf('\t$(MKDIR_RULE)')
io.printf('')
-- Mac OS X specific targets
if os.is("MacOSX") and prj.kind == "WindowedApp" then
io.printf('$(dir $(TARGETDIR))PkgInfo:')
io.printf('$(dir $(TARGETDIR))Info.plist:')
io.printf('')
end
-- clean target
io.printf('clean:')
io.printf('\t@echo Cleaning %s', prj.name)
io.printf('ifeq (posix,$(SHELLTYPE))')
io.printf('\t$(SILENT) rm -f $(TARGET)')
io.printf('\t$(SILENT) rm -rf $(OBJDIR)')
io.printf('else')
io.printf('\t$(SILENT) if exist $(subst /,\\\\,$(TARGET)) del $(subst /,\\\\,$(TARGET))')
io.printf('\t$(SILENT) if exist $(subst /,\\\\,$(OBJDIR)) rmdir /s /q $(subst /,\\\\,$(OBJDIR))')
io.printf('endif')
io.printf('')
-- custom build step targets
io.printf('prebuild:')
io.printf('\t$(PREBUILDCMDS)')
io.printf('')
io.printf('prelink:')
io.printf('\t$(PRELINKCMDS)')
io.printf('')
-- per-file rules
for _, file in ipairs(prj.files) do
if path.iscppfile(file) then
io.printf('$(OBJDIR)/%s.o: %s', _MAKE.esc(path.getbasename(file)), _MAKE.esc(file))
io.printf('\t@echo $(notdir $<)')
if (path.iscfile(file)) then
io.printf('\t$(SILENT) $(CC) $(CFLAGS) -o $@ -c $<')
else
io.printf('\t$(SILENT) $(CXX) $(CXXFLAGS) -o $@ -c $<')
end
elseif (path.getextension(file) == ".rc") then
io.printf('$(OBJDIR)/%s.res: %s', _MAKE.esc(path.getbasename(file)), _MAKE.esc(file))
io.printf('\t@echo $(notdir $<)')
io.printf('\t$(SILENT) windres $< -O coff -o $@ $(RESFLAGS)')
end
end
io.printf('')
-- include the dependencies, built by GCC (with the -MMD flag)
io.printf('-include $(OBJECTS:%%.o=%%.d)')
end