-
Notifications
You must be signed in to change notification settings - Fork 4
/
buildsys.html
1139 lines (980 loc) · 36.7 KB
/
buildsys.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-02-12 Fri 10:56 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>moo 無 build</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Brett Viren" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: auto;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline; margin-top: 14px;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="other/readtheorg/css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="other/readtheorg/css/readtheorg.css"/>
<script type="text/javascript" src="other/lib/js/jquery.min.js"></script>
<script type="text/javascript" src="other/lib/js/bootstrap.min.js"></script>
<script type="text/javascript" src="other/lib/js/jquery.stickytableheaders.min.js"></script>
<script type="text/javascript" src="other/readtheorg/js/readtheorg.js"></script>
<style> #content{max-width:1800px;}</style>
<style> p{max-width:800px;}</style>
<style> li{max-width:800px;}</style>
<style> pre.src{border-radius: 5px; background-color:#333; color:#0f0;}</style>
<style> pre.example{border-radius: 5px; background-color:#333; color:#0f0;}</style>
<style> code{border-radius: 5px; background-color:#333; color:#0f0;}</style>
<script type="text/javascript">
// @license magnet:?xt=urn:btih:e95b018ef3580986a04669f1b5879592219e2a7a&dn=public-domain.txt Public Domain
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.add("code-highlighted");
target.classList.add("code-highlighted");
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.remove("code-highlighted");
target.classList.remove("code-highlighted");
}
}
/*]]>*///-->
// @license-end
</script>
</head>
<body>
<div id="content">
<h1 class="title">moo 無 build
<br />
<span class="subtitle">Integrating <code>moo</code> with build systems</span>
</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#intro">Introduction</a>
<ul>
<li><a href="#manual">Manual codegen</a></li>
<li><a href="#integrated">Build integration</a></li>
</ul>
</li>
<li><a href="#inputs">Inputs</a></li>
<li><a href="#outputs">Outputs</a>
<ul>
<li><a href="#paths"><code>paths</code></a></li>
<li><a href="#resolve"><code>resolve</code></a></li>
<li><a href="#imports"><code>imports</code></a></li>
<li><a href="#compile"><code>compile</code></a></li>
<li><a href="#render"><code>render</code></a></li>
</ul>
</li>
<li><a href="#examples">Examples</a>
<ul>
<li><a href="#waf">Waf</a></li>
<li><a href="#make">Make</a></li>
<li><a href="#cmake">CMake</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-intro" class="outline-2">
<h2 id="intro">Introduction</h2>
<div class="outline-text-2" id="text-intro">
<p>
One of the primary use for <code>moo</code> is to generate code. So, naturally
it must integrate into code build systems and this note collects
methods to do just that.
</p>
<div class="note" id="orgb9bb368">
<p>
<code>moo</code> can also be used in generating a build system. See for example
the <a href="wcup.html">wcup</a> document which describes the use of <code>moo</code>
to generate a software package skeleton including its build system.
</p>
</div>
</div>
<div id="outline-container-manual" class="outline-3">
<h3 id="manual">Manual codegen</h3>
<div class="outline-text-3" id="text-manual">
<p>
A reasonably simple way to "integrate" <code>moo</code> into a build system is to
not. One may run <code>moo</code> manually, or partly automated via a shell
script, and arrange for the generated files to be placed in the source
areas and committed. Absent any apparent knowledge of <code>moo</code>, the
software project will simply appear to have an impeccably consistent
code developer.
</p>
<p>
Pros:
</p>
<ul class="org-ul">
<li>requires <code>moo</code> only at develop time, not build (nor run) time.</li>
<li>easy to implement</li>
<li>(manually) assures correct build dependencies</li>
<li>generated code can be placed alongside hand-made code which assists in browsing/understanding of the code base</li>
<li>building the project is decoupled from development occurring in <code>moo</code> itself or in templates provided by <code>moo</code> that are used by the project</li>
</ul>
<p>
Cons:
</p>
<ul class="org-ul">
<li>a developer can change a local template or model and forget to regenerate (though presumably, this is caught as a matter-of-course).</li>
</ul>
</div>
</div>
<div id="outline-container-integrated" class="outline-3">
<h3 id="integrated">Build integration</h3>
<div class="outline-text-3" id="text-integrated">
<p>
Another reasonable solution is to run <code>moo</code> commands as part of the
native build of the package. From the point of view of developing a
build system, <code>moo</code> is seen as a <b>compiler</b> or possibly more
accurately a <b>pre processor</b>.
</p>
<p>
While <code>g++</code> may convert C++ files to <code>.o</code> object files, <code>moo</code> converts <code>.j2</code>
and <code>.jsonnet</code> files into <code>.hpp</code>, <code>.cpp</code>, etc files (which then themselves
must go on to be further compiled. Thus, <code>moo</code> commands must be
<b>inserted</b> into the build graph that the build system (Waf, CMake, etc)
creates in a more novel manner than merely adding more source files.
</p>
<p>
The "pros" and "cons" are largely reversed from the manual option with the following additional "pros":
</p>
<ul class="org-ul">
<li>correct build dependencies may be assured, given a quality build system</li>
<li>generated code may be placed in or outside the source area</li>
</ul>
<p>
The rest of this document gives information to help build system
developers understand how to fit <code>moo</code> into their system. It focuses on
what input and output files with which <code>moo</code> may interact as most build
systems use a file-based dependency graph. It ends with some <a href="#examples">Examples</a> of integrating <code>moo</code> into a few of the more popular build systems (which are also understood by the author).
</p>
</div>
</div>
</div>
<div id="outline-container-inputs" class="outline-2">
<h2 id="inputs">Inputs</h2>
<div class="outline-text-2" id="text-inputs">
<p>
<code>moo</code> takes input from files in a number of different ways:
</p>
<dl class="org-dl">
<dt>models</dt><dd>these files may be provided in many different formats.
The format used for <code>moo</code> examples and which is recommended is
<code>.jsonnet</code>. But <code>moo</code> also supports the consumption of <code>.json</code>, <code>.xml</code>,
<code>.ini</code>, <code>.yaml</code>, <code>.csv</code> and others. Models are provided to <code>moo</code> via a
command line positional argument.</dd>
<dt>templates</dt><dd>these files conventionally have a "double extension"
such as <code>.hpp.j2</code>. The first (<code>.hpp</code>) signifies the target file
extension and the second (<code>.j2</code>) identifies the file as being marked
up with Jinja2 templates. The first part is free, while some <code>moo</code>
functionality is sensitive to the trailing <code>.j2</code>. Templates are
provided to <code>moo</code> via a command line positional argument.</dd>
<dt>graft</dt><dd>data may be added to the model data structure by specifying
its location via <a href="https://tools.ietf.org/html/rfc6901">JSON Pointer</a> syntax and a file that provides the
value. The file will be evaluated based on its file extension and
then "grafted" into the location. Grafts are performed with the
<code>moo -g <ptr>:<file></code> option.</dd>
<dt>TLAs</dt><dd>when a model in a <code>.jsonnet</code> file evaluates to a function, the
function arguments aka "top-level arguments" (TLAs) may be provided
with the <code>moo -A var=<value></code> command line argument. The <code><value></code> may
be code in Jsonnet syntax or a file name. If the latter, the file
will be loaded (as a model is) and its resulting data will provide
the <code><value></code>.</dd>
</dl>
<p>
<code>moo</code> can be given these files via an absolute or relative path. If a
relative path is given then <code>moo</code> will check for the file in list of
given absolute "search paths". Template and model search paths are
specified independently via command line arguments:
</p>
<pre class="example" id="orgb7f8229">
$ moo -M /path/to/models -M /path/to/more/models \
-T /path/to/templates -T /path/to/more/templates \
render a/relative/model.jsonnet /an/absolute/template.xyz.j2
</pre>
<p>
When using the <code>moo</code> CLI, one may also set these search paths by setting
<code>MOO_LOAD_PATH</code> and <code>MOO_TEMPLATE_PATH</code> environment variables.
</p>
<pre class="example" id="orgcbe02f4">
$ export MOO_LOAD_PATH=/path/to/models:/path/to/more/models
$ export MOO_TEMPLATE_PATH=/path/to/templates:/path/to/more/templates
$ moo render a/relative/model.jsonnet /an/absolute/template.xyz.j2
</pre>
<p>
Besides explicit files provided as <code>moo</code> command arguments (as above
example) <code>moo</code> may also receive inputs via two other channels:
</p>
<p>
For example, let's prepare three models where <code>a.jsonnet</code> is a function
and the other two are simple objects.
</p>
<div class="org-src-container">
<pre class="src src-shell">echo 'function(a=42) {x:a}' > buildsys/a.jsonnet
echo '{b:69}' > buildsys/b.jsonnet
echo '{c:"hello"}' > buildsys/c.jsonnet
</pre>
</div>
<p>
Here is an example of grafting:
</p>
<div class="org-src-container">
<pre class="src src-shell">moo -g /greetings:buildsys/c.jsonnet compile buildsys/b.jsonnet
</pre>
</div>
<div class="org-src-container">
<pre class="src src-json">{
"b": 69,
"greetings": {
"c": "hello"
}
}
</pre>
</div>
<p>
Here is an example were we inject model <code>b</code> via TLA where <code>b</code> is found via
a model search path set by <code>-M</code>.
</p>
<div class="org-src-container">
<pre class="src src-shell">moo -M buildsys -A a=b.jsonnet compile buildsys/a.jsonnet
</pre>
</div>
<div class="org-src-container">
<pre class="src src-json">{
"x": {
"b": 69
}
}
</pre>
</div>
<p>
Here is an example where we supply the injected data as a literal representation:
</p>
<div class="org-src-container">
<pre class="src src-shell">moo -A a='{name:"moo"}' compile buildsys/a.jsonnet
</pre>
</div>
<div class="org-src-container">
<pre class="src src-json">{
"x": {
"name": "moo"
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-outputs" class="outline-2">
<h2 id="outputs">Outputs</h2>
<div class="outline-text-2" id="text-outputs">
<p>
<code>moo</code> has several commands and each produce specific output. Here we
give a tour of the commands relevant to a build system and their
output files.
</p>
</div>
<div id="outline-container-paths" class="outline-3">
<h3 id="paths"><code>paths</code></h3>
<div class="outline-text-3" id="text-paths">
<p>
When integrating <code>moo</code> to a build system it can be useful to query what
search paths <code>moo</code> will use to locate a given file and <code>moo path</code> will
report these.
</p>
<p>
It is important to know that <code>moo</code> builds a search path depending on the
type of file to be found. For now, there are two classes of file
search paths: those to locate data files (includes both <i>model</i> and
<i>schema</i> files) and those to locate template files. The latter are
identified by a file name ending in <code>j2</code> and any other ending will be
considered a data file.
</p>
<p>
In addition, for either class of files, the complete search path is
composed of a <i>builtin</i> path and augmented with any additional path
given by the user.
</p>
<p>
Some examples makes this clear. First, display the <i>builtin</i> search
paths:
</p>
<div class="org-src-container">
<pre class="src src-shell">moo path j2
</pre>
</div>
<pre class="example" id="orge4ed93e">
/home/bv/dev/moo/moo/templates
</pre>
<div class="org-src-container">
<pre class="src src-shell">moo path jsonnet
</pre>
</div>
<pre class="example" id="org7938ff1">
/home/bv/dev/moo/moo/jsonnet-code
</pre>
<p>
Next we show how the user may augment the search paths.
</p>
<div class="org-src-container">
<pre class="src src-shell">moo -M /tmp path jsonnet
</pre>
</div>
<pre class="example" id="orge5d1890">
/home/bv/dev/moo/moo/jsonnet-code
/tmp
</pre>
<div class="org-src-container">
<pre class="src src-shell">moo -T $HOME/dev/moo/test path j2
</pre>
</div>
<pre class="example" id="orgcf6c50b">
/home/bv/dev/moo/moo/templates
/home/bv/dev/moo/test
</pre>
</div>
</div>
<div id="outline-container-resolve" class="outline-3">
<h3 id="resolve"><code>resolve</code></h3>
<div class="outline-text-3" id="text-resolve">
<p>
Like the <code>path</code> command, a build system integrator may wish to test if a
file is properly found without necessarily processing anything. The
<code>moo resolve</code> command goes the extra step:
</p>
<div class="org-src-container">
<pre class="src src-shell">moo resolve ocpp.hpp.j2
moo resolve moo.jsonnet
moo -M $HOME/dev/moo/test resolve test-any.jsonnet
</pre>
</div>
<pre class="example" id="org6230dc5">
/home/bv/dev/moo/moo/templates/ocpp.hpp.j2
/home/bv/dev/moo/moo/jsonnet-code/moo.jsonnet
/home/bv/dev/moo/test/test-any.jsonnet
</pre>
</div>
</div>
<div id="outline-container-imports" class="outline-3">
<h3 id="imports"><code>imports</code></h3>
<div class="outline-text-3" id="text-imports">
<p>
The <code>moo</code> command <code>imports</code> is provided to help discover intermediate
dependency files which have been imported through Jsonnet's or Jinja's
file inclusion mechanisms. This is analogous to CPP's <code>-M</code> option to
produce <code>.d</code> files for <code>make</code>.
</p>
<p>
Here is an example of how a build system may call <code>moo</code> to "scan" a
Jsonnet file so that the build dependency graph may properly insert
the implicit dependencies:
</p>
<div class="org-src-container">
<pre class="src src-shell">echo 'local a = import "b.jsonnet"; a' > buildsys/top.jsonnet
echo '{b:69}' > buildsys/b.jsonnet
</pre>
</div>
<div class="org-src-container">
<pre class="src src-shell">moo imports buildsys/top.jsonnet
</pre>
</div>
<pre class="example" id="org2f68456">
/home/bv/dev/moo/buildsys/b.jsonnet
</pre>
<p>
When a model is a function, all TLAs that lack default values must be
satisfied in order for evaluation to succeed. Thus all TLAs must be
satisfied when the <code>compile</code>, <code>render</code> or <code>imports</code> commands are used.
</p>
<div class="org-src-container">
<pre class="src src-shell">moo -A more=less imports buildsys/topf.jsonnet
</pre>
</div>
<pre class="example" id="org98b4e8f">
/home/bv/dev/moo/buildsys/b.jsonnet
/home/bv/dev/moo/buildsys/top.jsonnet
</pre>
<p>
A Jinja template file can be scanned similarly and will be detected as
such if its file name ends in <code>.j2</code>.
</p>
<div class="org-src-container">
<pre class="src src-shell">moo imports buildsys/top.txt.j2
</pre>
</div>
<pre class="example" id="org3d60289">
</pre>
<p>
By default, <code>moo</code> will print the list of imported files to standard
output. The <code>-o</code> option to <code>imports</code> can be provided to output to a
specific file.
</p>
</div>
</div>
<div id="outline-container-compile" class="outline-3">
<h3 id="compile"><code>compile</code></h3>
<div class="outline-text-3" id="text-compile">
<p>
<code>moo compile</code> will take a model in Jsonnet or other format and output
JSON. This takes the same main options as <code>render</code> (next) and so is a
way test the intermediate result of grafting and/or applying TLAs or
simply providing a common file type from a variety of supported input
types.
</p>
<p>
A build system may choose to explicitly expose this intermediate
representation but that must come at a cost of more file I/O and file
production. It should only be employed if needed.
</p>
</div>
</div>
<div id="outline-container-render" class="outline-3">
<h3 id="render"><code>render</code></h3>
<div class="outline-text-3" id="text-render">
<p>
The main goal of <code>moo</code> is to generate code and this is the main command
do perform that. Like <code>compile</code> (and <code>imports</code>) it prints the result to
standard output but can also write to an explicit file given with the
<code>-o</code> command option. A minimal example to show the command line is:
</p>
<pre class="example" id="org18a62e4">
moo render -o output.hpp model.jsonnet template.hpp.j2
</pre>
</div>
</div>
</div>
<div id="outline-container-examples" class="outline-2">
<h2 id="examples">Examples</h2>
<div class="outline-text-2" id="text-examples">
<p>
This section gives a tour of some playground style integration of <code>moo</code>
into various build systems.
</p>
</div>
<div id="outline-container-waf" class="outline-3">
<h3 id="waf">Waf</h3>
<div class="outline-text-3" id="text-waf">
<p>
Both Waf and <code>moo</code> are implemented in Python and that gives the
developers options in how to integrate <code>moo</code> into their Waf-based
build system. One may either call <code>moo</code> as a CLI program (see section
<a href="#waf-cmd">cmd</a>) or use it as a Python module (see section <a href="#waf-mod">mod</a>).
</p>
</div>
<div id="outline-container-waf-cmd" class="outline-4">
<h4 id="waf-cmd">cmd</h4>
<div class="outline-text-4" id="text-waf-cmd">
<p>
The Waf project in <a href="buildsys/waf/cmd/">buildsys/waf/cmd/</a> provides an example of integrating <code>moo</code>
as a command line program. It uses <code>moo render</code> to apply a model to a
simple template that merely dumps the data. The model is composed
through Jsonnet <code>import</code> mechanism and so <code>moo import</code> is also as a Waf
<i>scanner</i> to determine intermediate dependencies.
</p>
<p>
Here is the <code>wscript</code> file:
</p>
<div class="org-src-container">
<pre class="src src-python">#!/usr/bin/env waf
'''
An example waf wscript file for exercising moo as a command
'''
from subprocess import check_output
from waflib.Utils import subst_vars
def configure(cfg):
cfg.find_program('moo', var='MOO', mandatory=True)
def import_scanner(task):
deps = []
for node in task.inputs:
cmd = "${MOO} imports %s" % node.abspath()
cmd = subst_vars(cmd, task.env)
#out = task.exec_command(cmd)
out = check_output(cmd.split()).decode()
deps += out.split("\n")
deps = [task.generator.bld.path.find_or_declare(d) for d in deps if d]
print(deps)
return (deps, [])
def build(bld):
bld(rule="${MOO} render -o ${TGT} ${SRC}",
source="model.jsonnet dump.txt.j2",
target="model-dump.txt",
scan=import_scanner)
</pre>
</div>
<p>
Here is the "cmd" example being exercising. We do an initial build.
A second rebuild is a no-op. We then edit an intermediate file and
rebuild and notice that indeed the change is noticed.
</p>
<div class="org-src-container">
<pre class="src src-shell">cd buildsys/waf/cmd/
waf distclean configure
sed -i 's/fortran/FORTRAN/' sub/sub.jsonnet
echo -e "\nfirst build\n"
waf
echo -e "\nsecond build, no rebuild\n"
waf
echo -e "\nmodify intermediate dependency, see it rebuild\n"
sed -i 's/FORTRAN/fortran/' sub/sub.jsonnet
waf
</pre>
</div>
<pre class="example" id="org19fd884">
'distclean' finished successfully (0.002s)
Setting top to : /home/bv/dev/moo/buildsys/waf/cmd
Setting out to : /home/bv/dev/moo/buildsys/waf/cmd/build
Checking for program 'moo' : /home/bv/dev/moo/.direnv/python-venv-3.8.0/bin/moo
'configure' finished successfully (0.003s)
first build
Waf: Entering directory `/home/bv/dev/moo/buildsys/waf/cmd/build'
[/home/bv/dev/moo/buildsys/waf/cmd/sub.jsonnet, /home/bv/dev/moo/buildsys/waf/cmd/sub/sub.jsonnet]
[1/1] Processing model-dump.txt: model.jsonnet dump.txt.j2 -> build/model-dump.txt
Waf: Leaving directory `/home/bv/dev/moo/buildsys/waf/cmd/build'
'build' finished successfully (1.572s)
second build, no rebuild
Waf: Entering directory `/home/bv/dev/moo/buildsys/waf/cmd/build'
Waf: Leaving directory `/home/bv/dev/moo/buildsys/waf/cmd/build'
'build' finished successfully (0.014s)
modify intermediate dependency, see it rebuild
Waf: Entering directory `/home/bv/dev/moo/buildsys/waf/cmd/build'
[/home/bv/dev/moo/buildsys/waf/cmd/sub.jsonnet, /home/bv/dev/moo/buildsys/waf/cmd/sub/sub.jsonnet]
[1/1] Processing model-dump.txt: model.jsonnet dump.txt.j2 -> build/model-dump.txt
Waf: Leaving directory `/home/bv/dev/moo/buildsys/waf/cmd/build'
'build' finished successfully (1.585s)
</pre>
</div>
</div>
<div id="outline-container-waf-mod" class="outline-4">
<h4 id="waf-mod">mod</h4>
<div class="outline-text-4" id="text-waf-mod">
<p>
The second example works the same but instead of calling <code>moo</code> as a CLI
program, the Python module is used. In principle, this can be better
performing but it does that require <code>waf</code> is called with a version of Python
that is supported by <code>moo</code>.
</p>
<p>
Here is the <code>wscript</code> file for the "mod" example:
</p>
<div class="org-src-container">
<pre class="src src-python">#!/usr/bin/env waf
'''
An example waf wscript file for exercising moo as a Python module
This should be improved by turning the moo parts into a Waf tool!
'''
import moo
def import_scanner(task):
srcdir = task.generator.bld.path.abspath()
deps = []
for node in task.inputs:
deps += moo.imports(node.abspath(), srcdir)
deps = [task.generator.bld.path.find_or_declare(d) for d in deps if d]
print(deps)
return (deps, [])
def render(task):
"${MOO} render -o ${TGT} ${SRC}",
srcdir = task.generator.bld.path.abspath()
out = task.outputs[0]
model = moo.io.load(task.inputs[0].abspath())
templ = task.inputs[1].abspath()
# keep compatibility with moo render
helper = moo.io.load(moo.util.resolve("moo.jsonnet"))
out.write(moo.templates.render(templ, dict(model=model, moo=helper)))
def configure(cfg):
pass
def build(bld):
bld(rule=render,
source="model.jsonnet dump.txt.j2",
target="model-dump.txt",
scan=import_scanner)
</pre>
</div>
<p>
And, here it is being exercised:
</p>
<div class="org-src-container">
<pre class="src src-shell">cd buildsys/waf/mod/
waf distclean configure
sed -i 's/fortran/FORTRAN/' sub/sub.jsonnet
echo -e "\nfirst build\n"
waf
echo -e "\nsecond build, no rebuild\n"
waf
echo -e "\nmodify intermediate dependency, see it rebuild\n"
sed -i 's/FORTRAN/fortran/' sub/sub.jsonnet
waf
</pre>
</div>
<pre class="example" id="org4072791">
'distclean' finished successfully (0.002s)
Setting top to : /home/bv/dev/moo/buildsys/waf/mod
Setting out to : /home/bv/dev/moo/buildsys/waf/mod/build
'configure' finished successfully (0.002s)
first build
Waf: Entering directory `/home/bv/dev/moo/buildsys/waf/mod/build'
[/home/bv/dev/moo/buildsys/waf/mod/sub.jsonnet, /home/bv/dev/moo/buildsys/waf/mod/sub/sub.jsonnet]
[1/1] Processing model-dump.txt: model.jsonnet dump.txt.j2 -> build/model-dump.txt
Waf: Leaving directory `/home/bv/dev/moo/buildsys/waf/mod/build'
'build' finished successfully (0.346s)
second build, no rebuild
Waf: Entering directory `/home/bv/dev/moo/buildsys/waf/mod/build'
Waf: Leaving directory `/home/bv/dev/moo/buildsys/waf/mod/build'
'build' finished successfully (0.005s)
modify intermediate dependency, see it rebuild
Waf: Entering directory `/home/bv/dev/moo/buildsys/waf/mod/build'
[/home/bv/dev/moo/buildsys/waf/mod/sub.jsonnet, /home/bv/dev/moo/buildsys/waf/mod/sub/sub.jsonnet]
[1/1] Processing model-dump.txt: model.jsonnet dump.txt.j2 -> build/model-dump.txt
Waf: Leaving directory `/home/bv/dev/moo/buildsys/waf/mod/build'
'build' finished successfully (0.347s)
</pre>
</div>
</div>
</div>
<div id="outline-container-make" class="outline-3">
<h3 id="make">Make</h3>
<div class="outline-text-3" id="text-make">
<p>
Ah, venerable <code>make</code>. This example currently does not capture
intermediate dependencies. Contributions/fixes welcome.
</p>
<p>
The <code>Makefile</code>:
</p>
<div class="org-src-container">
<pre class="src src-makefile">model-dump.txt : model.jsonnet dump.txt.j2
moo render -o $@ $^
clean:
rm -f model-dump.txt
</pre>
</div>
<p>
And it getting exercised:
</p>
<div class="org-src-container">
<pre class="src src-shell">cd buildsys/make
make clean
sed -i 's/fortran/FORTRAN/' sub/sub.jsonnet
make
echo -e '\nNext we edit the intermediate files and note no rebuild\n'
sed -i 's/FORTRAN/fortran/' sub/sub.jsonnet
make
echo -e '\nIn any case, first build gave us:\n'
ls -l model-dump.txt
cat model-dump.txt
</pre>
</div>
<pre class="example" id="org707f5ad">
rm -f model-dump.txt
moo render -o model-dump.txt model.jsonnet dump.txt.j2
Next we edit the intermediate files and note no rebuild
make: 'model-dump.txt' is up to date.
In any case, first build gave us:
-rw-rw-r-- 1 bv bv 93 Feb 12 10:56 model-dump.txt
sub = {'a': 1, 'b': 2, 'c': 'hello'}
subsub = {'a': 1, 'b': 2, 'c': 'hello', 'd': 'FORTRAN'}
</pre>
</div>
</div>
<div id="outline-container-cmake" class="outline-3">
<h3 id="cmake">CMake</h3>
<div class="outline-text-3" id="text-cmake">