1#----------------------------------------------------------------------------
2# Project:  Horizon
3# File:     CTR.commondefs.func.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: 36036 $
14#----------------------------------------------------------------------------
15
16
17#----------------------------------------------------------------------------
18# Function definitions
19#----------------------------------------------------------------------------
20
21
22public.GetBannerFile(config) =
23    private.banner = $(config.getDefaultBannerFile)
24    if $(defined CTR_BANNER_SPEC)
25        private.base = $(removesuffix $(CTR_BANNER_SPEC))
26        return $(addprefix $(config.getObjectDirectory)$(DIRSEP), $(addsuffixes $(EXT_BANNER), $(base)))
27    else
28        if $(defined CTR_BANNER)
29            return $(CTR_BANNER)
30        elseif $(or $(and $(defined CTR_NO_BANNER_ICON), $(equal $(CTR_NO_BANNER_ICON), true)), \
31                    $(and $(defined CTR_NO_BANNER), $(equal $(CTR_NO_BANNER), true))            )
32            return $(EMPTY)
33    return $(banner)
34
35public.GetIconFile(config) =
36    private.icon = $(config.getDefaultIconFile)
37    if $(defined CTR_BANNER_SPEC)
38        private.base     = $(removesuffix $(CTR_BANNER_SPEC))
39        return $(addprefix $(config.getObjectDirectory)$(DIRSEP), $(addsuffixes $(EXT_ICON), $(base)))
40    else
41        if $(defined CTR_ICON)
42            return $(CTR_ICON)
43        elseif $(and $(defined CTR_NO_BANNER_ICON), $(equal $(CTR_NO_BANNER_ICON), true))
44            return $(EMPTY)
45    return $(icon)
46
47public.GetMakeromOptions(config)=
48    private.romfs_root = $(if $(defined ROMFS_ROOT),$`(ROMFS_ROOT),$(EMPTY))
49    private.title      = $(if $(defined TITLE),$`(TITLE),CtrApp)
50
51    private.flags[] =
52        -DROMFS_ROOT=$(romfs_root)
53        -DTITLE=$(title)
54
55# disable for omake2
56#    if $(defined CTR_BANNER_SPEC)
57#        if $(or $(defined CTR_BANNER), $(defined CTR_ICON))
58#            eprintln($"CTR_BANNER CTR_ICON and CTR_BANNER_SPEC cannot be used simultaneously")
59#            exit(1)
60
61    BannerFile = $(GetBannerFile $(config))
62    IconFile = $(GetIconFile $(config))
63    if $(not $(equal $(BannerFile.length), 0))
64        flags += -banner $(BannerFile)
65        export flags
66
67    if $(not $(equal $(IconFile.length), 0))
68        flags += -icon $(IconFile)
69        export flags
70
71    return $(flags)
72
73
74
75
76
77
78
79
80
81
82
83
84#-------------------------------------------------------
85# AddRuleToMakeCci
86#
87# Overview
88#   |
89#
90# Global variable that is referenced
91#   None.
92#-------------------------------------------------------
93AddRuleToMakeCci(cci, elf, flags, depends) =
94    $(cci): $(makeDirectory $(dirname $(cci)))
95    $(cci): $(elf) $(depends) $(MAKEROM) build-romfs :value: $(getMtimeIfLarge $(elf) $(depends))
96        $(MAKEROM) -o $@ $< $(flags) -f card
97    return $(cci)
98
99#-------------------------------------------------------
100# AddRuleToMakeCclE
101#
102# Overview
103#   |
104#
105# Global variable that is referenced
106#   None.
107#-------------------------------------------------------
108AddRuleToMakeCclE(ccl_e, elf, r, flags, depends) =
109    $(ccl_e): $(makeDirectory $(dirname $(ccl_e)))
110    $(ccl_e): $(elf) $(r) $(depends) $(MAKEROM) build-romfs :value: $(getMtimeIfLarge $(elf) $(r) $(depends))
111        $(MAKEROM) -o $@ $< $(flags) -lr $(r) -f le -m 0x01000000
112    return $(ccl_e)
113
114#-------------------------------------------------------
115# AddRuleToMakeCclR
116#
117# Overview
118#   |
119#
120# Global variable that is referenced
121#   None.
122#-------------------------------------------------------
123AddRuleToMakeCclR(ccl_r, flags, depends) =
124    $(ccl_r): $(makeDirectory $(dirname $(ccl_r)))
125    $(ccl_r): $(depends) $(MAKEROM) build-romfs :value: $(getMtimeIfLarge $(depends))
126        $(MAKEROM) -o $@ $(flags) -f lr
127    return $(ccl_r)
128
129#-------------------------------------------------------
130# AddRuleToMakeCcl
131#
132# Overview
133#   |
134#
135# Global variable that is referenced
136#   None.
137#-------------------------------------------------------
138AddRuleToMakeCcl(ccl, e, r) =
139    $(ccl): $(makeDirectory $(dirname $(ccl)))
140    $(ccl): $(e) $(r) $(MAKEROM) build-romfs :value: $(getMtimeIfLarge $(e) $(r))
141        $(MAKEROM) -o $@ -le $(e) -lr $(r) -f list
142    return $(ccl)
143
144#-------------------------------------------------------
145# AddRuleToMakeCxi
146#
147# Overview
148#   |
149#
150# Global variable that is referenced
151#   None.
152#-------------------------------------------------------
153AddRuleToMakeCxi(cxi, elf, flags, depends) =
154    $(cxi): $(makeDirectory $(dirname $(cxi)))
155    $(cxi): $(elf) $(depends) $(MAKEROM) build-romfs :value: $(getMtimeIfLarge $(elf) $(depends))
156        $(MAKEROM) -o $@ $< $(flags)
157    return $(cxi)
158
159#-------------------------------------------------------
160# AddRuleToMakeCia
161#
162# Overview
163#   |
164#
165# Global variable that is referenced
166#   None.
167#-------------------------------------------------------
168AddRuleToMakeCia(cia, flags, depends) =
169    $(cia): $(makeDirectory $(dirname $(cia)))
170    $(cia): $(depends) $(MAKECIA) :value: $(getMtimeIfLarge $(depends))
171        $(MAKECIA) -o $@ $(flags)
172    return $(cia)
173
174#-------------------------------------------------------
175# AddRuleToMakeCfa
176#
177# Overview
178#   |
179#
180# Global variable that is referenced
181#   None.
182#-------------------------------------------------------
183AddRuleToMakeCfa(cfa, srcdir, flags, depends) =
184    $(cfa): $(makeDirectory $(dirname $(cfa)))
185    $(cfa): $(srcdir) $(depends) $(MAKEROM) :value: $(getMtimeIfLarge $(depends))
186        $(MAKEROM) -o $@ -DROMFS_ROOT=$(srcdir) $(flags) -f data
187    return $(cfa)
188
189#-------------------------------------------------------
190# AddRuleToMakeBanner
191#
192# Overview
193#   |
194#
195# Global variable that is referenced
196#   None.
197#-------------------------------------------------------
198AddRuleToMakeBanner(banner, icon, bsf) =
199    $(banner): $(makeDirectory $(dirname $(banner)))
200    $(icon):   $(makeDirectory $(dirname $(icon)))
201    $(banner) $(icon): $(bsf) $(MAKEBANNER)
202        $(MAKEBANNER) $< $(banner) $(icon)
203
204
205AddRuleToMakeCfaFromCias(cfa, flags, depends) =
206    $(cfa): $(makeDirectory $(dirname $(cfa)))
207    $(cfa): $(depends) $(MAKECIAARCHIVE) :value: $(getMtimeIfLarge $(depends))
208        $(MAKECIAARCHIVE) -o $@ $(flags)
209    return $(cfa)
210
211
212
213
214
215#-------------------------------------------------------
216# MakeCci
217#
218# Overview
219#   Create .cci from ELF
220#   Return the .cci path
221#
222# Global variable that is referenced
223#   None.
224#-------------------------------------------------------
225MakeCci(config, name, elf, flags, cci_depends) =
226    private.cci = $(config.getBinaryPath $(name), $(EXT_CCI))
227    return $(AddRuleToMakeCci $(cci),$(elf),$(flags), $(cci_depends))
228
229#-------------------------------------------------------
230# MakeCclE
231#
232# Overview
233#   Create the E portion of .ccl
234#   Return the path to the E portion of .ccl
235#
236# Global variable that is referenced
237#   None.
238#-------------------------------------------------------
239MakeCclE(config, name, elf, r, flags, depends) =
240    private.e = $(config.getBinaryPath $(name), $(EXT_CCL_E))
241    return $(AddRuleToMakeCclE $(e),$(elf),$(r),$(flags),$(depends))
242
243#-------------------------------------------------------
244# MakeCclR
245#
246# Overview
247#   Create the R portion of .ccl
248#   Return the path to the R portion of .ccl
249#
250# Global variable that is referenced
251#   None.
252#-------------------------------------------------------
253MakeCclR(config, name, flags, cci_depends) =
254    private.r = $(config.getBinaryPath $(name), $(EXT_CCL_R))
255    return $(AddRuleToMakeCclR $(r),$(flags),$(cci_depends))
256
257#-------------------------------------------------------
258# MakeCcl
259#
260# Overview
261#   Create .ccl
262#   Return the .ccl path
263#
264# Global variable that is referenced
265#   None.
266#-------------------------------------------------------
267MakeCcl(config, name, e, r) =
268    private.ccl = $(config.getBinaryPath $(name), $(EXT_CCL))
269    return $(AddRuleToMakeCcl $(ccl),$(e),$(r))
270
271#-------------------------------------------------------
272# MakeCxi
273#
274# Overview
275#   Create .cxi from ELF
276#   Return the .cxi path
277#
278# Global variable that is referenced
279#   None.
280#-------------------------------------------------------
281MakeCxi(config, name, elf, flags, cxi_depends) =
282    private.cxi = $(config.getBinaryPath $(name), $(EXT_CXI))
283    return $(AddRuleToMakeCxi $(cxi),$(elf),$(flags), $(cxi_depends))
284
285#-------------------------------------------------------
286# MakeCia
287#
288# Overview
289#   Create .cia from .cxi
290#   Return the .cia path
291#
292# Global variable that is referenced
293#   None.
294#-------------------------------------------------------
295MakeCia(config, name, flags, depends) =
296    private.cia = $(config.getBinaryPath $(name), $(EXT_CIA))
297    return $(AddRuleToMakeCia $(cia), $(flags), $(depends))
298
299#-------------------------------------------------------
300# MakeCfa
301#
302# Overview
303#   Create .cfa from the directory
304#   Return the .cfa path
305#
306# Global variable that is referenced
307#   None.
308#-------------------------------------------------------
309MakeCfa(config, name, srcdir, flags, depends) =
310    private.cfa = $(config.getBinaryPath $(name), $(EXT_CFA))
311    return $(AddRuleToMakeCfa $(cfa), $(srcdir), $(flags), $(depends))
312
313
314#-------------------------------------------------------
315# MakeBanner
316#
317# Overview
318#   |
319#
320# Global variable that is referenced
321#   None.
322#-------------------------------------------------------
323MakeBanner(config, rsf) =
324    private.banner  = $(GetBannerFile $(config))
325    private.icon    = $(GetIconFile $(config))
326    return $(AddRuleToMakeBanner $(banner),$(icon),$(bsf))
327
328
329
330
331
332
333
334
335
336
337
338#-------------------------------------------------------
339# BuildCci
340#
341# Overview
342#   Create .cci
343#   Return the .cci path
344#
345# Global variable that is referenced
346#   None.
347#-------------------------------------------------------
348BuildCci(config, name, files, cci_flags, cci_depends) =
349    private.elf = $(ExecutableElf $(config), $(name), $(files))
350    private.cci = $(MakeCci $(config), $(name), $(elf), $(cci_flags), $(cci_depends))
351    return $(cci)
352
353#-------------------------------------------------------
354# BuildCcl
355#
356# Overview
357#   Create .ccl
358#   Return the .ccl path
359#
360# Global variable that is referenced
361#   None.
362#-------------------------------------------------------
363BuildCcl(config, name, files, cci_flags, cci_depends, r_depends) =
364    private.elf = $(ExecutableElf $(config), $(name), $(files))
365    private.r   = $(MakeCclR $(config), $(name), $(cci_flags), $(r_depends))
366    private.e   = $(MakeCclE $(config), $(name), $(elf), $(r), $(cci_flags), $(cci_depends))
367    private.ccl = $(MakeCcl $(config), $(name), $(e), $(r))
368    return $(ccl)
369
370#-------------------------------------------------------
371# BuildCia
372#
373# Overview
374#   Create .cia
375#   Return the .cia path
376#
377# Global variable that is referenced
378#   None.
379#-------------------------------------------------------
380BuildCia(config, name, files, cxi_flags, cia_flags, cxi_depends, cia_depends) =
381    if $(files)
382        private.elf = $(ExecutableElf $(config), $(name), $(files))
383        private.cxi = $(MakeCxi $(config), $(name), $(elf), $(cxi_flags), $(cxi_depends))
384        cia_flags   = -i $(cxi) $(cia_flags)
385        cia_depends += $(cxi)
386        export cia_flags
387        export cia_depends
388
389    return $(MakeCia $(config), $(name), $(cia_flags), $(cia_depends))
390
391
392
393MakeCfaFromCias(config, name, cia_list, rsf) =
394    private.workdir = $(makeDirectory $(makePath $(config.getObjectDirectory) $(name)))
395    private.cfa     = $(makePath $(config.getObjectDirectory) $(name)$(EXT_CFA))
396    private.flags   = -cia $(cia_list) -rsf $(rsf) --romfs-root $(workdir)
397    private.depends = $(cia_list) $(rsf) $(workdir)
398    return $(AddRuleToMakeCfaFromCias $(cfa), $(flags), $(depends))
399
400MakeChildCfa(config, cia_list) =
401    private.rsf = $(makePath $(ROOT_RESOURCES) specfiles Child.rsf)
402    return $(MakeCfaFromCias $(config), Child, $(cia_list), $(rsf))
403
404MakeManualCfa(config, manual_dir, flags) =
405    private.rsf = $(makePath $(ROOT_RESOURCES) specfiles Manual.rsf)
406    flags += -rsf $(rsf)
407    return $(MakeCfa $(config), Manual, $(manual_dir), $(flags), $(rsf))
408
409#-------------------------------------------------------
410# InstallProgram
411#
412# Overview
413#   |
414#
415# Global variable that is referenced
416#   BUILDTYPES
417#-------------------------------------------------------
418InstallProgram(config, install_dir, files) =
419    if $(not $(files))
420        return $(EMPTY)
421
422    if $(or $(and $(config.isSdkTool),\
423                  $(equal $(filter full,$(BUILDTYPES)),full)),\
424            $(and $(not $(config.isSdkTool)),\
425                  $(not $(equal $(install_dir), $(dirname $(nth 0, $(files)))))))
426        private.ofiles =
427            foreach(outfile, $(files))
428                dst = $(install_dir)/$(basename $(outfile))
429                value $(AddRuleToInstall $(dst),$(outfile))
430        return $(ofiles)
431    return $(EMPTY)
432
433ProgramCard(config, name, files, flags, depends) =
434    return $(BuildCci $(config),$(name),$(files),$(flags.find cci),$(depends.find cci))
435
436ProgramList(config, name, files, flags, depends) =
437    return $(BuildCcl $(config),$(name),$(files),$(flags.find cci),$(depends.find cci),$(depends.find rom_fs))
438
439ProgramImport(config, name, files, flags, depends) =
440    return $(BuildCia $(config),$(name),$(files), \
441        $(flags.find cxi),   $(flags.find cia),   \
442        $(depends.find cxi), $(depends.find cia) )
443
444#-------------------------------------------------------
445# Program
446#
447# Overview
448#   Create CTR execution file
449#
450# Global variable that is referenced
451#   BUILD_APPLICATION, LDSCRIPT_TEMPLATE, CRT_0_O,
452#   ROMFS_DEPENDENCIES, ROMFS_ROOT, MAKEROMFLAGS,
453#   DESCRIPTOR, ROM_SPEC_FILE, CTR_APPTYPE,
454#   PROGRAM_BUILDER_MAP, MANUAL_DIR, CHILD_APPS
455#-------------------------------------------------------
456Program(config, name, files) =
457    global.BUILD_APPLICATION = true
458    private.contents = $(Map)
459
460    if $(not $(defined LDSCRIPT_TEMPLATE))
461        LDSCRIPT_TEMPLATE = $(config.getLdscriptTemplatePath)
462        export
463
464    if $(not $(defined CRT_0_O))
465        CRT_0_O   = crt0.o
466        export
467
468    if $(not $(defined ROMFS_DEPENDENCIES))
469        if $(defined ROMFS_ROOT)
470            ROMFS_DEPENDENCIES = $(ls -R, $(dir $(ROMFS_ROOT)))
471            export
472        else
473            ROMFS_DEPENDENCIES =
474            export
475        export
476
477    # For manual
478    if $(defined MANUAL_DIR)
479        private.cfa = $(MakeManualCfa $(config), $(MANUAL_DIR), $(MAKEROMFLAGS))
480        contents = $(contents.add 1,$(cfa))
481        export contents
482
483    # For child
484    if $(defined CHILD_APPS)
485        private.cfa = $(MakeChildCfa $(config), $(CHILD_APPS))
486        contents = $(contents.add 2,$(cfa))
487        export contents
488
489    private.desc = $(if $(defined DESCRIPTOR),$`(DESCRIPTOR),$(config.getDefaultDescriptorPath))
490
491    private.cxi_flags   = $(MAKEROMFLAGS)
492    private.cci_flags   = $(EMPTY_ARRAY)
493    private.cia_flags   = $(MAKECIAFLAGS)
494    private.rom_fs_depends  = $(ROMFS_DEPENDENCIES)
495    private.cxi_depends     = $(desc) $(ROM_SPEC_FILE) $(GetBannerFile $(config)) $(GetIconFile $(config))
496    private.cci_depends     = $(EMPTY_ARRAY)
497    private.cia_depends     = $(EMPTY_ARRAY)
498
499    cxi_flags   += -desc $(desc) -rsf $(ROM_SPEC_FILE)
500    cxi_flags   += $(GetMakeromOptions $(config))
501
502    cci_flags   += $(mapToArray $(contents),$(fun k,v,-content $(v):$(k)))
503    cia_flags   += $(mapToArray $(contents),$(fun k,v,-i $(v):$(k)))
504    cci_depends += $(mapToArray $(contents),$(fun k,v,$(v)))
505    cia_depends += $(mapToArray $(contents),$(fun k,v,$(v)))
506
507    private.flags   = $(Map)
508    private.depends = $(Map)
509    flags   = $(flags.add cxi,$(cxi_flags))
510    flags   = $(flags.add cci,$(cxi_flags) $(cci_flags))
511    flags   = $(flags.add cia,$(cia_flags))
512    depends = $(depends.add rom_fs,$(rom_fs_depends))
513    depends = $(depends.add cxi,$(rom_fs_depends) $(cxi_depends))
514    depends = $(depends.add cci,$(rom_fs_depends) $(cxi_depends) $(cci_depends))
515    depends = $(depends.add cia,$(cia_depends))
516
517    private.app_type =
518        if $(equal $(CTR_APPTYPE),$(APPTYPE_BOTH))
519            value $(APPTYPE_CARD) $(APPTYPE_NAND)
520        else
521            value $(CTR_APPTYPE)
522
523    private.target_files =
524        foreach (type, $(app_type))
525            private.builder = $(PROGRAM_BUILDER_MAP.find $(type))
526            if $(builder)
527                value $(apply $(builder), $(config), $(name), $(files), $(flags), $(depends))
528            else
529                eprintln($"Build Error: Unknown APPTYPE:" '$(type)')
530                exit(1)
531
532    target_files = $(stripEmpty $(target_files))
533
534    # If the tree is referenced externally, determine whether to install
535    private.install_dir = $(config.getImageDirectory true)
536    private.install_targets = $(InstallProgram $(config),$(install_dir),$(target_files))
537
538    return $(target_files) $(install_targets)
539
540
541#-------------------------------------------------------
542# Banner
543#
544# Overview
545#   Create banner
546#
547# Global variable that is referenced
548#   None.
549#-------------------------------------------------------
550Banner(config, bsf) =
551    return $(MakeBanner $(config),$(bsf))
552
553
554
555
556