1#----------------------------------------------------------------------------
2# Project:  Horizon
3# File:     utildefs.om
4#
5# Copyright (C)2009-2011 Nintendo Co., Ltd.  All rights reserved.
6#
7# These coded instructions, statements, and computer programs contain
8# proprietary information of Nintendo of America Inc. and/or Nintendo
9# Company Ltd., and are protected by Federal copyright law. They may
10# not be disclosed to third parties or copied or duplicated in any form,
11# in whole or in part, without the prior written consent of Nintendo.
12#
13# $Rev: 35508 $
14#----------------------------------------------------------------------------
15
16# This file is only function definitions
17# Do not add anything other than function definitions
18
19#----------------------------------------------------------------------------
20# Function definitions
21#----------------------------------------------------------------------------
22
23#-------------------------------------------------------
24# gsub
25#
26# Overview
27#   |
28#
29# Referenced global variables
30#   FS, RS, stdout
31#-------------------------------------------------------
32public.gsub(str1, pattern, str2) =
33    FS=__XYZ
34    RS=__XYZ
35    stdout = $(open-out-string)
36    fsubst($(open-in-string $(str1)))
37    case $(pattern) g
38        value $(str2)
39    private.result = $(out-contents $(stdout))
40    close($(stdout))
41    return $(result)
42
43#-------------------------------------------------------
44# findstring
45#
46# Overview
47#   |
48#
49# Referenced global variables
50#   None.
51#-------------------------------------------------------
52public.findstring(str1, pattern) =
53    return $(grep q, $(pattern), $(open-in-string $(str1)))
54
55#-------------------------------------------------------
56# compileToRegex
57#
58# Overview
59#   |
60#
61# Referenced global variables
62#  stdout
63#-------------------------------------------------------
64public.compileToRegex(filterstrings) =
65    private.regex_result =
66        foreach(fs, $(filterstrings))
67            stdout = $(open-out-string)
68            fsubst($(open-in-string $(fs)))
69            case $'\*(\.\*)+' g
70                value $'*'
71            case $'\.' g
72                value $'\.'
73            case $'\*' g
74                value $'.*'
75            case $'[()]' g
76                value $'\'$1            # '
77
78            private.regex = $(out-contents $(stdout))
79            close($(stdout))
80            value $"\($(regex)\)"
81    return $(concat $'|', $(regex_result))
82
83#-------------------------------------------------------
84# isFilterMatch
85#
86# Overview
87#   |
88#
89# Referenced global variables
90#   None.
91#-------------------------------------------------------
92public.isFilterMatch(regex, fullname) =
93    match $(fullname)
94    case $(regex)
95        return true
96    default
97        return false
98
99#-------------------------------------------------------
100# exist-dirs
101#
102# Overview
103#   |
104#
105# Referenced global variables
106#   None.
107#-------------------------------------------------------
108public.exist-dirs(dirs) =
109    return $(filter $(dirs),$(glob D,*))
110
111#-------------------------------------------------------
112# ls_
113#
114# Overview
115#   |
116#
117# Referenced global variables
118#   None.
119#-------------------------------------------------------
120public.ls_(options, path) =
121    private.files = $(EMPTY)
122    .SUBDIRS: $(path)
123        files = $(ls $(options), .)
124        export files
125    return $(files)
126
127#-------------------------------------------------------
128# subdirs_
129#
130# Overview
131#   |
132#
133# Referenced global variables
134#   None.
135#-------------------------------------------------------
136public.subdirs_(options, path) =
137    private.dirs = $(EMPTY)
138    .SUBDIRS: $(path)
139        dirs = $(subdirs $(options), .)
140        export dirs
141    return $(dirs)
142
143#-------------------------------------------------------
144# ls_relative
145#
146# Overview
147#   |
148#
149# Referenced global variables
150#   None.
151#-------------------------------------------------------
152public.ls_relative(options, rdir) =
153    private.adir = $(absname $(rdir))
154    return $(removeprefix $(adir)$(DIRSEP), $(filter-out $(adir), $(ls_ $(options), $(adir))))
155
156#-------------------------------------------------------
157# files
158#
159# Overview
160#   |
161#
162# Referenced global variables
163#   GLOBAL_TEMP_RESULT
164#-------------------------------------------------------
165public.files(options, path) =
166    section
167        GLOBAL_TEMP_RESULT = $(EMPTY)
168        export GLOBAL_TEMP_RESULT
169        .SUBDIRS: $(path)
170            foreach(filename, $(ls $(options), .))
171                if $(test -f $(filename))
172                    GLOBAL_TEMP_RESULT += $(string $(filename))
173        return $(GLOBAL_TEMP_RESULT)
174
175#-------------------------------------------------------
176# filter-dirs
177#
178# Overview
179#   |
180#
181# Referenced global variables
182#   None.
183#-------------------------------------------------------
184public.filter-dirs(filenames) =
185    private.result = $(EMPTY)
186    section
187        export result
188        foreach(filename, $(filenames))
189            if $(test -d $(filename))
190                result += $(filename)
191    return $(result)
192
193#-------------------------------------------------------
194# filter-files
195#
196# Overview
197#   |
198#
199# Referenced global variables
200#   None.
201#-------------------------------------------------------
202public.filter-files(filenames) =
203    private.result = $(EMPTY)
204    section
205        export result
206        foreach(filename, $(filenames))
207            if $(test -f $(filename))
208                result += $(filename)
209    return $(result)
210
211#-------------------------------------------------------
212# CheckProg_
213#
214# Overview
215#   |
216#
217# Referenced global variables
218#   None.
219#-------------------------------------------------------
220public.CheckProg_(prog) =
221   private.where = $(where $(prog))
222   if $(where)
223      return true
224   else
225      return false
226
227#-------------------------------------------------------
228# readfile
229#
230# Overview
231#   |
232#
233# Referenced global variables
234#   None.
235#-------------------------------------------------------
236public.readfile(filename) =
237    private.result = $(EMPTY)
238    try
239        channel = $(fopen $(filename), r)
240        result += $(input-line $(channel))
241        close($(channel))
242        export result
243    catch RuntimeException(e)
244        return
245    return $(result)
246
247#-------------------------------------------------------
248# makeDirectory
249#
250# Overview
251#   |
252#
253# Referenced global variables
254#   None.
255#-------------------------------------------------------
256public.makeDirectory(path) =
257    $(path):
258        mkdir -p $@
259    return $(dir $(path))
260
261#-------------------------------------------------------
262# getMtimeIfLarge
263#
264# Overview
265#   |
266#
267# Referenced global variables
268#   None.
269#-------------------------------------------------------
270public.getMtimeIfLarge(path_list) =
271    private.result =
272        foreach (path, $(path_list))
273            if $(file-exists $(path))
274                private.file_stat = $(stat $(path))
275                value $(if $(ge $(file_stat.size), 8000000),$(file_stat.mtime),1)
276            else
277                value 0
278    return $(result)
279
280#-------------------------------------------------------
281# makePath
282#
283# Overview
284#   |
285#
286# Referenced global variables
287#   None.
288#-------------------------------------------------------
289public.makePath(a) =
290    return $(concat $(DIRSEP),$(a))
291
292#-------------------------------------------------------
293# stripEmpty
294#
295# Overview
296#   |
297#
298# Referenced global variables
299#   GLOBAL_TEMP_B
300#-------------------------------------------------------
301public.stripEmpty(a) =
302    section
303        GLOBAL_TEMP_B = $(EMPTY)
304        export GLOBAL_TEMP_B
305        foreach (e, $(a))
306            GLOBAL_TEMP_B += $(e)
307    return $(GLOBAL_TEMP_B)
308
309#-------------------------------------------------------
310# isEmpty
311#
312# Overview
313#   |
314#
315# Referenced global variables
316#   None.
317#-------------------------------------------------------
318public.isEmpty(seq) =
319    return $(eq 0, $(length $(seq)))
320
321#-------------------------------------------------------
322# digest-in-path-optional_internal
323#
324# Overview
325#   Internal function for digest-in-path-optional
326#
327# Referenced global variables
328#   None.
329#-------------------------------------------------------
330digest-in-path-optional_internal(path, file) =
331    # Unnecessary to search for path if it is an absolute path
332    if $(equal $(fullname $(file)), $(absname $(file)))
333        private.path_digest = $(digest-optional $(file))
334        if $(not $(equal $(path_digest), false))
335            return $(file) $(path_digest)
336        return
337
338    # Relative path
339    foreach(dir, $(path))
340        private.filepath = $(dir)$(DIRSEP)$(file)
341        export filepath
342        if $(equal ., $(dirof $(filepath)))
343            filepath = $(basename $(file))
344        else
345            filepath = $(dirof $(filepath))$(DIRSEP)$(basename $(filepath))
346
347        private.path_digest = $(digest-optional $(filepath))
348        if $(not $(equal $(path_digest), false))
349            return $(filepath) $(path_digest)
350    return
351
352#-------------------------------------------------------
353# digest-in-path-optional_
354#
355# Overview
356#   Version of digest-in-path-optional that does not reference cache
357#
358# Referenced global variables
359#   GLOBAL_TEMP_RESULT
360#-------------------------------------------------------
361public.digest-in-path-optional_(path, files) =
362    # To test the current path first
363    path = . $(path)
364
365    GLOBAL_TEMP_RESULT =
366    foreach(filename, $(files))
367        GLOBAL_TEMP_RESULT += $(digest-in-path-optional_internal $(path), $(filename))
368        export GLOBAL_TEMP_RESULT
369
370    return $(GLOBAL_TEMP_RESULT)
371
372
373
374
375
376public.mapToArray(m, f) =
377    GLOBAL_TEMP_RESULT = $(EMPTY_ARRAY)
378    section
379        export GLOBAL_TEMP_RESULT
380        m.foreach(k,v)
381            GLOBAL_TEMP_RESULT += $(apply $(f),$(k),$(v))
382    return $(GLOBAL_TEMP_RESULT)
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404