Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 6089de4

Browse files
author
William McLendon
committed
Improve examples and work on RTD landing page
1 parent cdef325 commit 6089de4

File tree

7 files changed

+200
-54
lines changed

7 files changed

+200
-54
lines changed

README.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,13 @@ be provided in the `opt-set-cmake-var` operation in the .ini file:
292292
SetProgramOptionsCMake Examples
293293
-------------------------------
294294

295-
### [example-02.ini](examples/example-02.ini)
295+
### Example 02
296+
297+
**example-02.ini**
296298
```ini
299+
#
300+
# example-02.ini
301+
#
297302
[CMAKE_COMMAND]
298303
opt-set cmake
299304

@@ -304,9 +309,9 @@ opt-set -G : Ninja
304309
opt-set -G : "Unix Makefiles"
305310

306311
[MYPROJ_OPTIONS]
307-
opt-set-cmake-var MYPROJ_CXX_FLAGS : "-O0 -fopenmp"
308-
opt-set-cmake-var MYPROJ_ENABLE_OPTION_A BOOL FORCE : ON
309-
opt-set-cmake-var MYPROJ_ENABLE_OPTION_B BOOL PARENT_SCOPE : ON
312+
opt-set-cmake-var MYPROJ_CXX_FLAGS STRING : "-O0 -fopenmp"
313+
opt-set-cmake-var MYPROJ_ENABLE_OPTION_A BOOL FORCE : ON
314+
opt-set-cmake-var MYPROJ_ENABLE_OPTION_B BOOL : ON
310315

311316
[MYPROJ_SOURCE_DIR]
312317
opt-set /path/to/source/dir
@@ -324,18 +329,22 @@ use MYPROJ_OPTIONS
324329
use MYPROJ_SOURCE_DIR
325330
```
326331

327-
### [example-02.py](examples/example-02.py)
332+
**example-02.py**
328333
```python
329334
#!/usr/bin/env python3
330-
import os
331-
import sys
332-
335+
# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
336+
from pathlib import Path
333337
import setprogramoptions
334338

339+
print(80*"-")
340+
print(f"- {Path(__file__).name}")
341+
print(80*"-")
342+
343+
335344
filename = "example-02.ini"
336345
popts = setprogramoptions.SetProgramOptionsCMake(filename)
337346

338-
section = "MYPROJ_CONFIGURATION_NINJA"
347+
section = "MYPROJ_CONFIGURATION_NINJA"
339348
popts.parse_section(section)
340349

341350
# Generate BASH output
@@ -357,26 +366,27 @@ print("\nDone")
357366
Generates the output:
358367
```bash
359368
$ python3 example-02.py
369+
--------------------------------------------------------------------------------
370+
- example-02.py
371+
--------------------------------------------------------------------------------
360372

361-
Bash output
362-
-----------
373+
**Bash output**
363374
cmake \
364375
-G=Ninja \
365-
-DMYPROJ_CXX_FLAGS="-O0 -fopenmp" \
376+
-DMYPROJ_CXX_FLAGS:STRING="-O0 -fopenmp" \
366377
-DMYPROJ_ENABLE_OPTION_A:BOOL=ON \
367378
-DMYPROJ_ENABLE_OPTION_B:BOOL=ON \
368379
/path/to/source/dir
369380

370381
CMake fragment output
371382
---------------------
372-
set(MYPROJ_CXX_FLAGS "-O0 -fopenmp")
383+
set(MYPROJ_CXX_FLAGS "-O0 -fopenmp" CACHE STRING "from .ini configuration")
373384
set(MYPROJ_ENABLE_OPTION_A ON CACHE BOOL "from .ini configuration" FORCE)
374-
set(MYPROJ_ENABLE_OPTION_B ON CACHE BOOL "from .ini configuration" PARENT_SCOPE)
385+
set(MYPROJ_ENABLE_OPTION_B ON CACHE BOOL "from .ini configuration")
375386

376387
Done
377388
```
378389

379-
380390
[1]: https://cmake.org/cmake/help/latest/command/set.html
381391
[2]: https://github.com/sandialabs/ConfigParserEnhanced
382392
[3]: https://github.com/sandialabs/SetProgramOptions/blob/master/CHANGELOG.md

doc/source/index.rst

Lines changed: 159 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,177 @@
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
Welcome to SetProgramOptions documentation!
7-
===========================================
6+
SetProgramOptions Python Module
7+
===============================
88

99
.. toctree::
10-
:maxdepth: 2
11-
:caption: Contents:
10+
:maxdepth: 1
11+
:caption: Table of Contents:
1212

1313
SetProgramOptions
1414
SetProgramOptionsCMake
1515
License <License>
1616

1717

1818

19-
Indices and tables
20-
==================
19+
Indices and Tables
20+
------------------
2121

2222
* :ref:`genindex`
2323
* :ref:`modindex`
2424
* :ref:`search`
2525

2626

27+
Overview
28+
========
29+
TODO: This needs to be expanded for RTD.
30+
31+
32+
Examples
33+
========
34+
Here we show a few examples of how ``SetProgramOptions`` and
35+
``SetProgramOptionsCMake`` can be used.
36+
37+
Example 1
38+
---------
39+
In example-01 we have a fairly simple .ini file that demostrates utilization
40+
of the ``use`` operation that comes built in with
41+
`ConfigParserEnhanced <https://pypi.org/project/configparserenhanced/>`_.
42+
In this example we are going to demonstrate how the ``opt-set`` operations
43+
in our .ini file can be used to generate a custom bash command with
44+
customizable argument sets added.
45+
46+
In this case, we will process the ``[MY_LS_COMMAND]`` section to generate
47+
a bash command that would generate a directory listing in list form by
48+
reverse time order of last modified with a custom timestamp format.
49+
50+
While this example is quite simple we can see how a complex environment
51+
in a DevOps setting might use this to bundle "common" operations to reduce
52+
the amount of copying and pasting that is used.
53+
54+
example-01.ini
55+
++++++++++++++
56+
.. literalinclude:: ../../examples/example-01.ini
57+
:language: ini
58+
:linenos:
59+
60+
example-01.py
61+
+++++++++++++
62+
.. literalinclude:: ../../examples/example-01.py
63+
:language: python
64+
:linenos:
65+
66+
Console Output
67+
++++++++++++++
68+
.. literalinclude:: ../../examples/example-01.log
69+
:language: text
70+
:linenos:
71+
72+
Example 2
73+
---------
74+
Example 2 demonstrates the use of ``SetProgramOptionsCMake``
75+
which is a *subclass* of ``SetProgramOptions`` and implements
76+
*operations* for handling *CMake* variables in a .ini file.
77+
SetProgramOptionsCMake also introduces a new "CMake fragment"
78+
generator and implements logic to maintain consistency of
79+
behavior between generated BASH scripts and CMake fragments
80+
with knoweldge of how CMake treats ``-D`` operations at the
81+
command line versus ``set()`` operations inside a CMake script.
82+
83+
Here we show the new operation ``opt-set-cmake-var`` which
84+
has the form ``opt-set-cmake-var VARNAME <OPTIONAL PARAMS> : VALUE``.
85+
Details and a comprehensive list of the optional parameters can
86+
be found in the ``SetProgramOptionsCMake`` Class Reference page.
87+
88+
The main elements this example will demonstrate is how to use
89+
``opt-set-cmake-var`` operations and how they can be used.
90+
91+
This example is fairly straightforward since the .ini file
92+
options being defined will generate the same output for both
93+
generator types.
94+
95+
example-02.ini
96+
++++++++++++++
97+
.. literalinclude:: ../../examples/example-02.ini
98+
:language: ini
99+
:linenos:
100+
101+
example-02.py
102+
+++++++++++++
103+
.. literalinclude:: ../../examples/example-02.py
104+
:language: python
105+
:linenos:
106+
107+
Console Output
108+
++++++++++++++
109+
.. literalinclude:: ../../examples/example-02.log
110+
:language: text
111+
:linenos:
112+
113+
114+
115+
Example 3
116+
---------
117+
The last example we show here is a bit more complicated and gets into
118+
some of the differences in how CMake treats a *command line* variable
119+
being set via a ``-D`` option versus a ``set()`` operation within a
120+
CMakeLists.txt file.
121+
122+
The script prints out a notice explaining the nuance but the general
123+
idea is that CMake treats options provided by a ``-D`` option at the
124+
command line as though they are ``CACHE`` variables with the ``FORCE``
125+
option enabled. This is designed to allow command-line parameters to
126+
generally take precedence over what a CMakeLists.txt file might set
127+
as though it's a user-override. The added ``FORCE`` option also ensures
128+
that if there are multiple ``-D`` options setting the same variable the
129+
*last* one will win.
130+
131+
This can have a subtle yet profound effect on how we must process our
132+
``opt-set-cmake-var`` operations within a .ini file if our goal is to
133+
ensure that the resulting ``CMakeCache.txt`` file generated by a CMake
134+
run would be the same for both *bash* and *cmake fragment* generators.
135+
136+
In this case, in order to have a variable be set by the *bash* generator
137+
it must be a ``CACHE`` variable -- which can be accomplished by either
138+
adding a *TYPE* or a *FORCE* option.
139+
We will note here that if ``FORCE`` is given without a *TYPE* then we
140+
use the default type of *STRING*.
141+
142+
If the *same* CMake variable is being assigned, such as in a case where
143+
we have a section that is updating a flag, then the `FORCE` option must
144+
be present on the second and all subsequent occurrences of ``opt-set-cmake-var``
145+
or the *bash* generator will skip over that assignment since a non-forced
146+
``set()`` operation in CMake would not overwrite an existing cache var.
147+
This situation can occur frequently if our .ini file(s) are structured to
148+
have some *common* configuration option set and then a specialization which
149+
updates one of the arguments. One example of this kind of situation might
150+
be where we have a specialization that adds OpenMP and we would want to add
151+
the ``-fopenmp`` flags to our linker flags.
152+
153+
example-03.ini
154+
++++++++++++++
155+
.. literalinclude:: ../../examples/example-03.ini
156+
:language: ini
157+
:linenos:
158+
159+
example-03.py
160+
+++++++++++++
161+
.. literalinclude:: ../../examples/example-03.py
162+
:language: python
163+
:linenos:
164+
165+
Console Output
166+
++++++++++++++
167+
.. literalinclude:: ../../examples/example-03.log
168+
:language: text
169+
:linenos:
170+
171+
We will note here that the *CMake* fragment generator will still generate
172+
all of the commands. In this case the second ``set()`` command would be ignored
173+
by CMake since it's not FORCEd but the main take-away here is that the
174+
*bash* generator omitted the second ``-D`` operation since that would be
175+
a ``FORCE`` operation by default which is not representative of what was
176+
specified in the .ini file.
177+
178+
179+

examples/example-01.ini

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
#==============================================================================
2-
# SetProgramOptions Example
3-
#==============================================================================
4-
5-
6-
[BASH_VERSION]
7-
opt-set bash
8-
opt-set --version
9-
1+
#
2+
# example-01.ini
3+
#
104
[LS_COMMAND]
115
opt-set ls
126

examples/example-02.ini

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ opt-set cmake
77
[CMAKE_GENERATOR_NINJA]
88
opt-set -G : Ninja
99

10-
[CMAKE_GENERATOR_MAKEFILES]
11-
opt-set -G : "Unix Makefiles"
12-
1310
[MYPROJ_OPTIONS]
1411
opt-set-cmake-var MYPROJ_CXX_FLAGS STRING : "-O0 -fopenmp"
1512
opt-set-cmake-var MYPROJ_ENABLE_OPTION_A BOOL FORCE : ON
@@ -23,9 +20,3 @@ use CMAKE_COMMAND
2320
use CMAKE_GENERATOR_NINJA
2421
use MYPROJ_OPTIONS
2522
use MYPROJ_SOURCE_DIR
26-
27-
[MYPROJ_CONFIGURATION_MAKEFILES]
28-
use CMAKE_COMMAND
29-
use CMAKE_GENERATOR_MAKEFILES
30-
use MYPROJ_OPTIONS
31-
use MYPROJ_SOURCE_DIR

examples/example-02.log

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
- example-02.py
33
--------------------------------------------------------------------------------
44

5-
Bash output
6-
-----------
5+
Generate Bash Output
6+
--------------------
77
cmake \
88
-G=Ninja \
99
-DMYPROJ_CXX_FLAGS:STRING="-O0 -fopenmp" \
1010
-DMYPROJ_ENABLE_OPTION_A:BOOL=ON \
1111
-DMYPROJ_ENABLE_OPTION_B:BOOL=ON \
1212
/path/to/source/dir
1313

14-
CMake fragment output
15-
---------------------
14+
Generate CMake Fragment
15+
-----------------------
1616
set(MYPROJ_CXX_FLAGS "-O0 -fopenmp" CACHE STRING "from .ini configuration")
1717
set(MYPROJ_ENABLE_OPTION_A ON CACHE BOOL "from .ini configuration" FORCE)
1818
set(MYPROJ_ENABLE_OPTION_B ON CACHE BOOL "from .ini configuration")

examples/example-02.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
from pathlib import Path
44
import setprogramoptions
55

6+
def print_separator(label):
7+
print("")
8+
print(f"{label}")
9+
print("-"*len(label))
10+
return
11+
612
print(80*"-")
713
print(f"- {Path(__file__).name}")
814
print(80*"-")
915

10-
1116
filename = "example-02.ini"
1217
popts = setprogramoptions.SetProgramOptionsCMake(filename)
1318

1419
section = "MYPROJ_CONFIGURATION_NINJA"
1520
popts.parse_section(section)
1621

1722
# Generate BASH output
18-
print("")
19-
print("Bash output")
20-
print("-----------")
23+
print_separator("Generate Bash Output")
2124
bash_options = popts.gen_option_list(section, generator="bash")
2225
print(" \\\n ".join(bash_options))
2326

2427
# Generate a CMake Fragment
25-
print("")
26-
print("CMake fragment output")
27-
print("---------------------")
28+
print_separator("Generate CMake Fragment")
2829
cmake_options = popts.gen_option_list(section, generator="cmake_fragment")
2930
print("\n".join(cmake_options))
3031

examples/example-03.ini

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
# ------------------------------------------------------------------------------
21
#
3-
# Example with variable expansion with SetProgramOptionsCMake
2+
# example-03.ini
43
#
5-
# ------------------------------------------------------------------------------
6-
74
[TEST_VAR_EXPANSION_COMMON]
85
opt-set-cmake-var CMAKE_CXX_FLAGS STRING : "${LDFLAGS|ENV} -foo"
96

0 commit comments

Comments
 (0)