sdl_gen_config.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. -- Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
  2. --
  3. -- This software is provided 'as-is', without any express or implied
  4. -- warranty. In no event will the authors be held liable for any damages
  5. -- arising from the use of this software.
  6. --
  7. -- Permission is granted to anyone to use this software for any purpose,
  8. -- including commercial applications, and to alter it and redistribute it
  9. -- freely.
  10. --
  11. -- Meta-build system using premake created and maintained by
  12. -- Benjamin Henning <b.henning@digipen.edu>
  13. --[[
  14. sdl_gen_config.lua
  15. Given a series of set configuration values from the project definitions,
  16. this file contains a series of functions that generate valid preprocessor
  17. definitions to enable or disable various features of the SDL2 library. These
  18. definitions are pasted into a template SDL config header file, which is then
  19. saved in the local directory and referenced to in generated project files.
  20. This file depends on sdl_file.lua.
  21. ]]
  22. -- The line that must exist in the template file in order to properly paste
  23. -- the generated definitions.
  24. local searchKey = "/%* Paste generated code here %*/\n"
  25. local configFile, templateFileContents
  26. local insertLocation
  27. -- This function begins config header generation given the name of the generated
  28. -- file and the name of the template file to use.
  29. function startGeneration(file, template)
  30. configFile = fileopen(file, "wt")
  31. templateFileContents = readfile(template, "rt")
  32. insertLocation = templateFileContents:find(searchKey)
  33. if insertLocation then
  34. configFile:write(templateFileContents:sub(1, insertLocation - 1))
  35. end
  36. end
  37. -- Adds a table of configuration values to the generated file. Each
  38. -- configuration line is wrapped around a preprocessor definition check, so they
  39. -- can be manually overwritten by the developer if necessary. The definition
  40. -- pastes string versions of both the key and the value on the line, where
  41. -- either is allowed to be empty. That means the table stores key-value pairs.
  42. function addConfig(tbl)
  43. -- if no insert location, don't paste anything
  44. if not insertLocation then return end
  45. for k,v in pairs(tbl) do
  46. configFile:print(0, "#ifndef " .. k)
  47. configFile:print(0, "#define " .. tostring(k) .. " " .. tostring(v))
  48. configFile:print(0, "#endif")
  49. end
  50. end
  51. -- Finishes the generation and writes the remains of the template file into the
  52. -- generated config file.
  53. function endGeneration()
  54. if insertLocation then
  55. configFile:write(templateFileContents:sub(insertLocation + #searchKey - 2))
  56. else -- write entire file since nothing is being pasted
  57. configFile:write(templateFileContents)
  58. end
  59. configFile:close()
  60. end