2009-04-11 11:33:32 +00:00
|
|
|
--
|
|
|
|
-- make_cpp.lua
|
|
|
|
-- Generate a C/C++ project makefile.
|
2011-09-15 20:45:48 +00:00
|
|
|
-- Copyright (c) 2002-2011 Jason Perkins and the Premake project
|
2009-04-11 11:33:32 +00:00
|
|
|
--
|
|
|
|
|
2010-10-19 11:14:46 +00:00
|
|
|
premake.make.cpp = { }
|
2011-09-15 20:45:48 +00:00
|
|
|
local cpp = premake.make.cpp
|
|
|
|
local make = premake.make
|
2010-10-19 11:14:46 +00:00
|
|
|
|
|
|
|
|
2009-04-11 11:33:32 +00:00
|
|
|
function premake.make_cpp(prj)
|
|
|
|
-- create a shortcut to the compiler interface
|
2009-05-12 22:43:06 +00:00
|
|
|
local cc = premake.gettool(prj)
|
2009-04-11 11:33:32 +00:00
|
|
|
|
2009-04-20 19:06:48 +00:00
|
|
|
-- build a list of supported target platforms that also includes a generic build
|
2009-04-21 20:00:17 +00:00
|
|
|
local platforms = premake.filterplatforms(prj.solution, cc.platforms, "Native")
|
2009-04-20 19:06:48 +00:00
|
|
|
|
2009-05-01 19:50:45 +00:00
|
|
|
premake.gmake_cpp_header(prj, cc, platforms)
|
2009-04-11 11:33:32 +00:00
|
|
|
|
2009-04-14 19:41:30 +00:00
|
|
|
for _, platform in ipairs(platforms) do
|
|
|
|
for cfg in premake.eachconfig(prj, platform) do
|
2009-04-24 14:34:54 +00:00
|
|
|
premake.gmake_cpp_config(cfg, cc)
|
2009-04-11 11:33:32 +00:00
|
|
|
end
|
|
|
|
end
|
2009-05-22 20:27:05 +00:00
|
|
|
|
2009-04-11 11:33:32 +00:00
|
|
|
-- list intermediate files
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('OBJECTS := \\')
|
2009-04-11 11:33:32 +00:00
|
|
|
for _, file in ipairs(prj.files) do
|
|
|
|
if path.iscppfile(file) then
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('\t$(OBJDIR)/%s.o \\', _MAKE.esc(path.getbasename(file)))
|
2009-04-11 11:33:32 +00:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('RESOURCES := \\')
|
2009-04-11 11:33:32 +00:00
|
|
|
for _, file in ipairs(prj.files) do
|
|
|
|
if path.isresourcefile(file) then
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('\t$(OBJDIR)/%s.res \\', _MAKE.esc(path.getbasename(file)))
|
2009-04-11 11:33:32 +00:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
2009-05-29 17:45:37 +00:00
|
|
|
-- identify the shell type
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('SHELLTYPE := msdos')
|
|
|
|
_p('ifeq (,$(ComSpec)$(COMSPEC))')
|
|
|
|
_p(' SHELLTYPE := posix')
|
|
|
|
_p('endif')
|
|
|
|
_p('ifeq (/bin,$(findstring /bin,$(SHELL)))')
|
|
|
|
_p(' SHELLTYPE := posix')
|
|
|
|
_p('endif')
|
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
|
|
|
-- main build rule(s)
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('.PHONY: clean prebuild prelink')
|
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
|
|
|
if os.is("MacOSX") and prj.kind == "WindowedApp" then
|
2009-05-22 20:27:05 +00:00
|
|
|
_p('all: $(TARGETDIR) $(OBJDIR) prebuild prelink $(TARGET) $(dir $(TARGETDIR))PkgInfo $(dir $(TARGETDIR))Info.plist')
|
2009-04-11 11:33:32 +00:00
|
|
|
else
|
2009-05-22 20:27:05 +00:00
|
|
|
_p('all: $(TARGETDIR) $(OBJDIR) prebuild prelink $(TARGET)')
|
2009-04-11 11:33:32 +00:00
|
|
|
end
|
2010-05-07 14:58:20 +00:00
|
|
|
_p('\t@:')
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('')
|
2009-05-22 20:27:05 +00:00
|
|
|
|
|
|
|
-- target build rule
|
|
|
|
_p('$(TARGET): $(GCH) $(OBJECTS) $(LDDEPS) $(RESOURCES)')
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('\t@echo Linking %s', prj.name)
|
2009-05-22 18:38:53 +00:00
|
|
|
_p('\t$(SILENT) $(LINKCMD)')
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('\t$(POSTBUILDCMDS)')
|
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
2009-05-29 17:45:37 +00:00
|
|
|
-- Create destination directories. Can't use $@ for this because it loses the
|
|
|
|
-- escaping, causing issues with spaces and parenthesis
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('$(TARGETDIR):')
|
2009-05-29 17:45:37 +00:00
|
|
|
premake.make_mkdirrule("$(TARGETDIR)")
|
2009-04-11 11:33:32 +00:00
|
|
|
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('$(OBJDIR):')
|
2009-05-29 17:45:37 +00:00
|
|
|
premake.make_mkdirrule("$(OBJDIR)")
|
2009-04-11 11:33:32 +00:00
|
|
|
|
|
|
|
-- Mac OS X specific targets
|
|
|
|
if os.is("MacOSX") and prj.kind == "WindowedApp" then
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('$(dir $(TARGETDIR))PkgInfo:')
|
|
|
|
_p('$(dir $(TARGETDIR))Info.plist:')
|
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- clean target
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('clean:')
|
|
|
|
_p('\t@echo Cleaning %s', prj.name)
|
|
|
|
_p('ifeq (posix,$(SHELLTYPE))')
|
|
|
|
_p('\t$(SILENT) rm -f $(TARGET)')
|
|
|
|
_p('\t$(SILENT) rm -rf $(OBJDIR)')
|
|
|
|
_p('else')
|
|
|
|
_p('\t$(SILENT) if exist $(subst /,\\\\,$(TARGET)) del $(subst /,\\\\,$(TARGET))')
|
|
|
|
_p('\t$(SILENT) if exist $(subst /,\\\\,$(OBJDIR)) rmdir /s /q $(subst /,\\\\,$(OBJDIR))')
|
|
|
|
_p('endif')
|
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
|
|
|
-- custom build step targets
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('prebuild:')
|
|
|
|
_p('\t$(PREBUILDCMDS)')
|
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('prelink:')
|
|
|
|
_p('\t$(PRELINKCMDS)')
|
|
|
|
_p('')
|
2009-05-22 20:27:05 +00:00
|
|
|
|
|
|
|
-- precompiler header rule
|
2011-09-15 20:45:48 +00:00
|
|
|
cpp.pchrules(prj)
|
2009-05-22 20:27:05 +00:00
|
|
|
|
2009-04-11 11:33:32 +00:00
|
|
|
-- per-file rules
|
|
|
|
for _, file in ipairs(prj.files) do
|
|
|
|
if path.iscppfile(file) then
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('$(OBJDIR)/%s.o: %s', _MAKE.esc(path.getbasename(file)), _MAKE.esc(file))
|
|
|
|
_p('\t@echo $(notdir $<)')
|
2011-09-19 20:45:49 +00:00
|
|
|
cpp.buildcommand(path.iscfile(file))
|
2009-04-11 11:33:32 +00:00
|
|
|
elseif (path.getextension(file) == ".rc") then
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('$(OBJDIR)/%s.res: %s', _MAKE.esc(path.getbasename(file)), _MAKE.esc(file))
|
|
|
|
_p('\t@echo $(notdir $<)')
|
2010-10-21 10:41:52 +00:00
|
|
|
_p('\t$(SILENT) windres $< -O coff -o "$@" $(RESFLAGS)')
|
2009-04-11 11:33:32 +00:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('')
|
2009-04-11 11:33:32 +00:00
|
|
|
|
|
|
|
-- include the dependencies, built by GCC (with the -MMD flag)
|
2009-04-11 11:49:07 +00:00
|
|
|
_p('-include $(OBJECTS:%%.o=%%.d)')
|
2009-04-11 11:33:32 +00:00
|
|
|
end
|
2009-04-23 18:26:31 +00:00
|
|
|
|
|
|
|
|
2009-05-01 19:50:45 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Write the makefile header
|
|
|
|
--
|
|
|
|
|
|
|
|
function premake.gmake_cpp_header(prj, cc, platforms)
|
2009-08-11 00:11:48 +00:00
|
|
|
_p('# %s project makefile autogenerated by Premake', premake.action.current().shortname)
|
2009-05-01 19:50:45 +00:00
|
|
|
|
|
|
|
-- set up the environment
|
|
|
|
_p('ifndef config')
|
|
|
|
_p(' config=%s', _MAKE.esc(premake.getconfigname(prj.solution.configurations[1], platforms[1], true)))
|
|
|
|
_p('endif')
|
|
|
|
_p('')
|
|
|
|
|
|
|
|
_p('ifndef verbose')
|
|
|
|
_p(' SILENT = @')
|
|
|
|
_p('endif')
|
|
|
|
_p('')
|
|
|
|
|
|
|
|
_p('ifndef CC')
|
|
|
|
_p(' CC = %s', cc.cc)
|
|
|
|
_p('endif')
|
|
|
|
_p('')
|
|
|
|
|
|
|
|
_p('ifndef CXX')
|
|
|
|
_p(' CXX = %s', cc.cxx)
|
|
|
|
_p('endif')
|
|
|
|
_p('')
|
|
|
|
|
|
|
|
_p('ifndef AR')
|
|
|
|
_p(' AR = %s', cc.ar)
|
|
|
|
_p('endif')
|
|
|
|
_p('')
|
|
|
|
end
|
|
|
|
|
2011-09-15 20:45:48 +00:00
|
|
|
|
2009-04-23 18:26:31 +00:00
|
|
|
--
|
|
|
|
-- Write a block of configuration settings.
|
|
|
|
--
|
|
|
|
|
2009-04-24 14:34:54 +00:00
|
|
|
function premake.gmake_cpp_config(cfg, cc)
|
2009-04-23 18:26:31 +00:00
|
|
|
|
2009-04-24 14:34:54 +00:00
|
|
|
_p('ifeq ($(config),%s)', _MAKE.esc(cfg.shortname))
|
2009-05-01 19:50:45 +00:00
|
|
|
|
2011-09-15 20:45:48 +00:00
|
|
|
-- if this platform requires a special compiler or linker, list it here
|
|
|
|
cpp.platformtools(cfg, cc)
|
2009-05-22 20:27:05 +00:00
|
|
|
|
|
|
|
_p(' OBJDIR = %s', _MAKE.esc(cfg.objectsdir))
|
2009-05-12 22:11:24 +00:00
|
|
|
_p(' TARGETDIR = %s', _MAKE.esc(cfg.buildtarget.directory))
|
2009-04-23 18:26:31 +00:00
|
|
|
_p(' TARGET = $(TARGETDIR)/%s', _MAKE.esc(cfg.buildtarget.name))
|
|
|
|
_p(' DEFINES += %s', table.concat(cc.getdefines(cfg.defines), " "))
|
|
|
|
_p(' INCLUDES += %s', table.concat(cc.getincludedirs(cfg.includedirs), " "))
|
2011-09-15 20:45:48 +00:00
|
|
|
|
|
|
|
-- CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS, and RESFLAGS
|
|
|
|
cpp.flags(cfg, cc)
|
2009-05-22 20:27:05 +00:00
|
|
|
|
|
|
|
-- set up precompiled headers
|
2011-09-15 20:45:48 +00:00
|
|
|
cpp.pchconfig(cfg)
|
2009-05-22 20:27:05 +00:00
|
|
|
|
2009-05-22 18:38:53 +00:00
|
|
|
_p(' LIBS += %s', table.concat(cc.getlinkflags(cfg), " "))
|
2009-04-23 18:26:31 +00:00
|
|
|
_p(' LDDEPS += %s', table.concat(_MAKE.esc(premake.getlinks(cfg, "siblings", "fullpath")), " "))
|
|
|
|
|
|
|
|
if cfg.kind == "StaticLib" then
|
2009-07-13 18:34:26 +00:00
|
|
|
if cfg.platform:startswith("Universal") then
|
|
|
|
_p(' LINKCMD = libtool -o $(TARGET) $(OBJECTS)')
|
|
|
|
else
|
|
|
|
_p(' LINKCMD = $(AR) -rcs $(TARGET) $(OBJECTS)')
|
|
|
|
end
|
2009-04-23 18:26:31 +00:00
|
|
|
else
|
2009-09-04 21:40:31 +00:00
|
|
|
-- this was $(TARGET) $(LDFLAGS) $(OBJECTS) ... but was having trouble linking to certain
|
|
|
|
-- static libraries so $(OBJECTS) was moved up
|
|
|
|
_p(' LINKCMD = $(%s) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(ARCH) $(LIBS)', iif(cfg.language == "C", "CC", "CXX"))
|
2009-04-23 18:26:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
_p(' define PREBUILDCMDS')
|
|
|
|
if #cfg.prebuildcommands > 0 then
|
|
|
|
_p('\t@echo Running pre-build commands')
|
|
|
|
_p('\t%s', table.implode(cfg.prebuildcommands, "", "", "\n\t"))
|
|
|
|
end
|
|
|
|
_p(' endef')
|
|
|
|
|
|
|
|
_p(' define PRELINKCMDS')
|
|
|
|
if #cfg.prelinkcommands > 0 then
|
|
|
|
_p('\t@echo Running pre-link commands')
|
|
|
|
_p('\t%s', table.implode(cfg.prelinkcommands, "", "", "\n\t"))
|
|
|
|
end
|
|
|
|
_p(' endef')
|
|
|
|
|
|
|
|
_p(' define POSTBUILDCMDS')
|
|
|
|
if #cfg.postbuildcommands > 0 then
|
|
|
|
_p('\t@echo Running post-build commands')
|
|
|
|
_p('\t%s', table.implode(cfg.postbuildcommands, "", "", "\n\t"))
|
|
|
|
end
|
|
|
|
_p(' endef')
|
|
|
|
|
2011-09-15 20:45:48 +00:00
|
|
|
-- write out config-level makesettings blocks
|
|
|
|
make.settings(cfg, cc)
|
|
|
|
|
2009-04-23 18:26:31 +00:00
|
|
|
_p('endif')
|
|
|
|
_p('')
|
|
|
|
end
|
2010-10-19 11:14:46 +00:00
|
|
|
|
|
|
|
|
2011-09-15 20:45:48 +00:00
|
|
|
--
|
|
|
|
-- Platform support
|
|
|
|
--
|
|
|
|
|
|
|
|
function cpp.platformtools(cfg, cc)
|
|
|
|
local platform = cc.platforms[cfg.platform]
|
|
|
|
if platform.cc then
|
|
|
|
_p(' CC = %s', platform.cc)
|
|
|
|
end
|
|
|
|
if platform.cxx then
|
|
|
|
_p(' CXX = %s', platform.cxx)
|
|
|
|
end
|
|
|
|
if platform.ar then
|
|
|
|
_p(' AR = %s', platform.ar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Configurations
|
|
|
|
--
|
|
|
|
|
|
|
|
function cpp.flags(cfg, cc)
|
|
|
|
_p(' CPPFLAGS += %s $(DEFINES) $(INCLUDES)', table.concat(cc.getcppflags(cfg), " "))
|
|
|
|
_p(' CFLAGS += $(CPPFLAGS) $(ARCH) %s', table.concat(table.join(cc.getcflags(cfg), cfg.buildoptions), " "))
|
|
|
|
_p(' CXXFLAGS += $(CFLAGS) %s', table.concat(cc.getcxxflags(cfg), " "))
|
2011-10-26 19:54:52 +00:00
|
|
|
|
|
|
|
-- Patch #3401184 changed the order
|
|
|
|
_p(' LDFLAGS += %s', table.concat(table.join(cc.getlibdirflags(cfg), cc.getldflags(cfg), cfg.linkoptions), " "))
|
|
|
|
|
2011-09-15 20:45:48 +00:00
|
|
|
_p(' RESFLAGS += $(DEFINES) $(INCLUDES) %s',
|
|
|
|
table.concat(table.join(cc.getdefines(cfg.resdefines),
|
|
|
|
cc.getincludedirs(cfg.resincludedirs), cfg.resoptions), " "))
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-10-19 11:14:46 +00:00
|
|
|
--
|
|
|
|
-- Precompiled header support
|
|
|
|
--
|
|
|
|
|
2011-10-25 22:06:53 +00:00
|
|
|
function cpp.pchconfig(cfg)
|
|
|
|
-- GCC needs the full path to the PCH, while Visual Studio needs
|
|
|
|
-- only the name (or rather, the name as specified in the #include
|
|
|
|
-- statement). Try to locate the PCH in the project.
|
|
|
|
local pchheader = cfg.pchheader
|
|
|
|
for _, incdir in ipairs(cfg.includedirs) do
|
|
|
|
local testname = path.join(incdir, cfg.pchheader)
|
|
|
|
if os.isfile(testname) then
|
|
|
|
pchheader = testname
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-19 11:14:46 +00:00
|
|
|
if not cfg.flags.NoPCH and cfg.pchheader then
|
|
|
|
_p(' PCH = %s', _MAKE.esc(path.getrelative(cfg.location, cfg.pchheader)))
|
|
|
|
_p(' GCH = $(OBJDIR)/%s.gch', _MAKE.esc(path.getname(cfg.pchheader)))
|
|
|
|
_p(' CPPFLAGS += -I$(OBJDIR) -include $(OBJDIR)/%s', _MAKE.esc(path.getname(cfg.pchheader)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-15 20:45:48 +00:00
|
|
|
function cpp.pchrules(prj)
|
2010-10-19 11:14:46 +00:00
|
|
|
_p('ifneq (,$(PCH))')
|
|
|
|
_p('$(GCH): $(PCH)')
|
|
|
|
_p('\t@echo $(notdir $<)')
|
2011-10-20 20:44:09 +00:00
|
|
|
_p('ifeq (posix,$(SHELLTYPE))')
|
2010-10-19 11:14:46 +00:00
|
|
|
_p('\t-$(SILENT) cp $< $(OBJDIR)')
|
2011-10-20 20:44:09 +00:00
|
|
|
_p('else')
|
|
|
|
_p('\t$(SILENT) xcopy /D /Y /Q "$(subst /,\\,$<)" "$(subst /,\\,$(OBJDIR))" 1>nul')
|
|
|
|
_p('endif')
|
2011-09-19 20:45:49 +00:00
|
|
|
cpp.buildcommand(prj.language == "C")
|
2010-10-19 11:14:46 +00:00
|
|
|
_p('endif')
|
|
|
|
_p('')
|
|
|
|
end
|
2011-09-19 20:45:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Build command for a single file.
|
|
|
|
--
|
|
|
|
|
|
|
|
function cpp.buildcommand(iscfile)
|
|
|
|
local flags = iif(iscfile, '$(CC) $(CFLAGS)', '$(CXX) $(CXXFLAGS)')
|
|
|
|
_p('\t$(SILENT) %s -o "$@" -MF $(@:%%.o=%%.d) -c "$<"', flags)
|
|
|
|
end
|
|
|
|
|
|
|
|
|