_premake_main.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. --
  2. -- _premake_main.lua
  3. -- Script-side entry point for the main program logic.
  4. -- Copyright (c) 2002-2011 Jason Perkins and the Premake project
  5. --
  6. _WORKING_DIR = os.getcwd()
  7. --
  8. -- Inject a new target platform into each solution; called if the --platform
  9. -- argument was specified on the command line.
  10. --
  11. local function injectplatform(platform)
  12. if not platform then return true end
  13. platform = premake.checkvalue(platform, premake.fields.platforms.allowed)
  14. for sln in premake.solution.each() do
  15. local platforms = sln.platforms or { }
  16. -- an empty table is equivalent to a native build
  17. if #platforms == 0 then
  18. table.insert(platforms, "Native")
  19. end
  20. -- the solution must provide a native build in order to support this feature
  21. if not table.contains(platforms, "Native") then
  22. return false, sln.name .. " does not target native platform\nNative platform settings are required for the --platform feature."
  23. end
  24. -- add it to the end of the list, if it isn't in there already
  25. if not table.contains(platforms, platform) then
  26. table.insert(platforms, platform)
  27. end
  28. sln.platforms = platforms
  29. end
  30. return true
  31. end
  32. --
  33. -- Script-side program entry point.
  34. --
  35. function _premake_main(scriptpath)
  36. -- if running off the disk (in debug mode), load everything
  37. -- listed in _manifest.lua; the list divisions make sure
  38. -- everything gets initialized in the proper order.
  39. if (scriptpath) then
  40. local scripts = dofile(scriptpath .. "/_manifest.lua")
  41. for _,v in ipairs(scripts) do
  42. dofile(scriptpath .. "/" .. v)
  43. end
  44. end
  45. local profiler = newProfiler()
  46. if (nil ~= _OPTIONS["debug-profiler"]) then
  47. profiler:start()
  48. end
  49. -- Now that the scripts are loaded, I can use path.getabsolute() to properly
  50. -- canonicalize the executable path.
  51. _PREMAKE_COMMAND = path.getabsolute(_PREMAKE_COMMAND)
  52. -- Set up the environment for the chosen action early, so side-effects
  53. -- can be picked up by the scripts.
  54. premake.action.set(_ACTION)
  55. -- Seed the random number generator so actions don't have to do it themselves
  56. math.randomseed(os.time())
  57. -- If there is a project script available, run it to get the
  58. -- project information, available options and actions, etc.
  59. if (nil ~= _OPTIONS["file"]) then
  60. local fname = _OPTIONS["file"]
  61. if (os.isfile(fname)) then
  62. dofile(fname)
  63. else
  64. error("No genie script '" .. fname .. "' found!", 2)
  65. end
  66. else
  67. local dir, name = premake.findDefaultScript(path.getabsolute("./"))
  68. if dir ~= nil then
  69. local cwd = os.getcwd()
  70. os.chdir(dir)
  71. dofile(name)
  72. os.chdir(cwd)
  73. end
  74. end
  75. -- Process special options
  76. if (_OPTIONS["version"] or _OPTIONS["help"] or not _ACTION) then
  77. printf("GENie - Project generator tool %s", _GENIE_VERSION_STR)
  78. printf("https://github.com/bkaradzic/GENie")
  79. if (not _OPTIONS["version"]) then
  80. premake.showhelp()
  81. end
  82. return 1
  83. end
  84. -- Validate the command-line arguments. This has to happen after the
  85. -- script has run to allow for project-specific options
  86. action = premake.action.current()
  87. if (not action) then
  88. error("Error: no such action '" .. _ACTION .. "'", 0)
  89. end
  90. ok, err = premake.option.validate(_OPTIONS)
  91. if (not ok) then error("Error: " .. err, 0) end
  92. -- Sanity check the current project setup
  93. ok, err = premake.checktools()
  94. if (not ok) then error("Error: " .. err, 0) end
  95. -- If a platform was specified on the command line, inject it now
  96. ok, err = injectplatform(_OPTIONS["platform"])
  97. if (not ok) then error("Error: " .. err, 0) end
  98. -- work-in-progress: build the configurations
  99. print("Building configurations...")
  100. premake.bake.buildconfigs()
  101. ok, err = premake.checkprojects()
  102. if (not ok) then error("Error: " .. err, 0) end
  103. premake.stats = { }
  104. premake.stats.num_generated = 0
  105. premake.stats.num_skipped = 0
  106. -- Hand over control to the action
  107. printf("Running action '%s'...", action.trigger)
  108. premake.action.call(action.trigger)
  109. if (nil ~= _OPTIONS["debug-profiler"]) then
  110. profiler:stop()
  111. local filePath = path.getabsolute("GENie-profile.txt")
  112. print("Writing debug-profile report to ''" .. filePath .. "'.")
  113. local outfile = io.open(filePath, "w+")
  114. profiler:report(outfile, true)
  115. outfile:close()
  116. end
  117. printf("Done. Generated %d/%d projects."
  118. , premake.stats.num_generated
  119. , premake.stats.num_generated+premake.stats.num_skipped
  120. )
  121. return 0
  122. end