sdl_depends.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. -- Copyright (C) 1997-2014 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. -- This is kept just for windows because the other platforms use different means
  14. -- for determining dependence or compatibility.
  15. --[[
  16. sdl_depends.lua
  17. This file simply contains a function for determining whether a dependency
  18. exists on the Windows platform, given a possible environmental variable,
  19. delimited search paths, and a main and/or sub-directory paths for more
  20. elaborate pattern matching.
  21. ]]
  22. -- find_dependency_dir_windows(env, main_search_path, main_dir_path)
  23. -- Attempt to resolve a dependency (true or false) folder based on either an
  24. -- environmental variable, start search path, or both. If both are present,
  25. -- the environmental variable will be preferred. If neither are present, this
  26. -- function returns false.
  27. --
  28. -- Arguments:
  29. -- env The name of the environmental variable to treat as a path
  30. -- main_search_paths Paths to look for the main directory in
  31. -- main_dir_path The a path that must be contained between main_search_path and sub_dir_path
  32. -- sub_dir_path The path of the directories that should exist at the searched path
  33. function find_dependency_dir_windows(env, main_search_paths, main_dir_path, sub_dir_path)
  34. if not os.is("windows") then -- if not windows, then fail
  35. return false
  36. end
  37. if env == nil and (main_search_paths == nil or #main_search_paths == 0) then
  38. return false
  39. end
  40. local env_path = nil
  41. local main_path = nil
  42. if env ~= nil then env_path = os.getenv(env) end
  43. local search_table = { n = 0 }
  44. if main_search_paths ~= nil then
  45. for k,main_search_path in ipairs(explode(main_search_paths, ";")) do
  46. local directories = os.matchdirs(main_search_path .. "/**" .. main_dir_path .. "*")
  47. for k,v in pairs(directories) do
  48. table.insert(search_table, v)
  49. end
  50. end
  51. end
  52. if env_path ~= nil then table.insert(search_table, env_path) end
  53. local search_path = table.concat(search_table, ";")
  54. local result_path = os.dirpathsearch(sub_dir_path, search_path, ";")
  55. if result_path == nil then
  56. return false
  57. end
  58. local found_dir = os.isdir(result_path)
  59. local abs_path = path.getabsolute(result_path)
  60. if found_dir and env_path ~= nil then
  61. abs_path = abs_path:gsub("\\", "/")
  62. env_path = env_path:gsub("\\", "/")
  63. local pos = abs_path:indexOf(env_path)
  64. if pos ~= nil then
  65. abs_path = abs_path:sub(1, pos - 1) .. "$(" .. env .. ")/" .. abs_path:sub(pos + #env_path)
  66. end
  67. end
  68. -- we want the path in terms of '/'
  69. return found_dir, abs_path
  70. end