|
143 | 143 | end |
144 | 144 | end |
145 | 145 |
|
| 146 | +--- |
| 147 | +-- Attempt to locate and return the path to a header file. |
| 148 | +-- |
| 149 | +-- Searches the same well-known system locations as os.findlib(), but replaces any /lib or /bin |
| 150 | +-- components with /include, and also searches in additional headerdirs provided by the caller. |
| 151 | +-- |
| 152 | +-- @param headerpath |
| 153 | +-- A partial header file path. |
| 154 | +-- @param headerdirs |
| 155 | +-- Optional; a string or table of additional paths. |
| 156 | +-- If an input is an absolute path, it will be searched directly. |
| 157 | +-- If an input is a relative path, it will be treated as is, relative to the current working directory. |
| 158 | +-- @return |
| 159 | +-- The full path to the directory containing the headerpath if found; `nil` otherwise. |
| 160 | +--- |
146 | 161 | function os.findheader(headerpath, headerdirs) |
147 | | - -- headerpath: a partial header file path |
148 | | - -- headerdirs: additional header search paths |
149 | | - |
150 | 162 | local paths = get_library_search_path() |
151 | 163 |
|
152 | 164 | -- replace all /lib and /bin by /include |
|
165 | 177 | return result |
166 | 178 | end |
167 | 179 |
|
| 180 | +--- |
| 181 | +-- Attempt to locate and return the path to a header file under additional subdirectories. |
| 182 | +-- |
| 183 | +-- Searches the same well-known system locations as os.findheader(), but appends additional |
| 184 | +-- subdirectory paths provided by the caller to the system search paths before searching. |
| 185 | +-- |
| 186 | +-- @param headerpath |
| 187 | +-- A partial header file path. |
| 188 | +-- @param additionalpaths |
| 189 | +-- Required; a string or table of additional paths. |
| 190 | +-- If an input is an absolute path, it will be searched directly. |
| 191 | +-- If an input is a relative path, it will be appended to each system search path separately. |
| 192 | +-- If an input is an empty string, the system search paths will be searched (path.join returns the original path). |
| 193 | +-- @return |
| 194 | +-- The full path to the directory containing the headerpath if found; `nil` otherwise. |
| 195 | +--- |
| 196 | + function os.findsubdirheader(headerpath, additionalpaths) |
| 197 | + if additionalpaths == nil then |
| 198 | + error("os.findsubdirheader: additionalpaths is required", 2) |
| 199 | + end |
| 200 | + |
| 201 | + local paths = get_library_search_path() |
| 202 | + |
| 203 | + -- replace all /lib and /bin by /include |
| 204 | + paths = table.translate(paths, function (p) return p:gsub('[/\\]lib[0-9]*', '/include'):gsub('[/\\]bin', '/include') end) |
| 205 | + |
| 206 | + local userpaths = {} |
| 207 | + |
| 208 | + if type(additionalpaths) == "string" then |
| 209 | + userpaths = { additionalpaths } |
| 210 | + elseif type(additionalpaths) == "table" then |
| 211 | + userpaths = additionalpaths |
| 212 | + end |
| 213 | + |
| 214 | + if #userpaths > 0 then |
| 215 | + local basepaths = paths |
| 216 | + local newpaths = {} |
| 217 | + for _, userpath in ipairs(userpaths) do |
| 218 | + if path.isabsolute(userpath) then |
| 219 | + -- absolute path: search directly, same as os.findheader |
| 220 | + table.insert(newpaths, userpath) |
| 221 | + else |
| 222 | + for _, p in ipairs(basepaths) do |
| 223 | + table.insert(newpaths, path.join(p, userpath)) |
| 224 | + end |
| 225 | + end |
| 226 | + end |
| 227 | + paths = newpaths |
| 228 | + end |
| 229 | + |
| 230 | + local result = os.pathsearch(headerpath, table.unpack(paths)) |
| 231 | + return result |
| 232 | + end |
| 233 | + |
168 | 234 | -- |
169 | 235 | -- Retrieve the current target operating system ID string. |
170 | 236 | -- |
|
0 commit comments