2020``-DVAR_NAME:BOOL=ON`` using the ``SetProgramOptions`` method ``gen_option_list``
2121with the ``bash`` generator.
2222
23- In the case of bash command entries the ``PARENT_SCOPE`` optional parameter is
24- ignored.
23+ When using the BASH generator to generate command line arguments, CMake
24+ uses the syntax ``-D<VARNAME>[:<TYPE>]=<VALUE>``. The ``TYPE`` field is optional
25+ and if left out CMake will default to a ``STRING`` type. Further, all CMake
26+ varaibles set via the command line using ``-D`` will be CACHE variables and each
27+ ``-D`` operation should be considered a FORCE operation too. For example,
28+ ``-DFOO:STRING=BAR`` is roughly equivalent to the CMake command:
29+ ``set(FOO CACHE STRING "docstring" FORCE)``.
30+
31+ The ``PARENT_SCOPE`` option applies only to non-cache variables and its presence
32+ will instruct CMake to make that variable non-cache. Care should be taken when
33+ using ``PARENT_SCOPE`` as combining it with the usual CACHE operations results
34+ in CMake creating a non-cached variable whose contents are the list containing
35+ ``<varname>;CACHE;<type>;doc string``.
2536
2637See CMake documentation on the `set() <https://cmake.org/cmake/help/latest/command/set.html>`_
2738command for more information on how fragment file entries are generated.
@@ -73,7 +84,6 @@ class ExpandVarsInTextCMake(ExpandVarsInText):
7384
7485 def __init__ (self ):
7586 self .exception_control_level = 3
76- self .exception_control_compact_warnings = True
7787
7888 def _fieldhandler_BASH_CMAKE (self , field ):
7989 """
@@ -183,7 +193,7 @@ def _program_option_handler_opt_set_cmake_fragment(self, params: list, value: st
183193 """
184194 return None
185195
186- def _program_option_handler_opt_set_cmake_var_bash (self , params , value ) -> str :
196+ def _program_option_handler_opt_set_cmake_var_bash (self , params : list , value : str ) -> str :
187197 """
188198 Line-item generator for ``opt-set-cmake-var`` entries when the *generator*
189199 is set to ``bash``.
@@ -196,30 +206,50 @@ def _program_option_handler_opt_set_cmake_var_bash(self, params, value) -> str:
196206 side-effects since :py:meth:`setprogramoptions.SetProgramOptions._gen_option_entry`
197207 performs a deep-copy of these parameters prior to calling this.
198208 Any changes we make are ephemeral.
209+
210+ Args:
211+ params (list): The parameters of the operation.
212+ value (str): The value of the option that is being assigned.
213+
214+ Raises:
215+ ValueError: This can potentially raise a ``ValueError`` if
216+ ``exception_control_level`` is set to 5 if there are
217+ operations that are skipped in Bash generation. If ``ecl``
218+ is less than 5 then warnings are generated to note the
219+ exclusion.
199220 """
200221 varname = params [0 ]
201222 params = params [1 : 4 ]
202223 param_opts = self ._helper_opt_set_cmake_var_parse_parameters (params )
203224
225+ # PARENT_SCOPE (i.e., type-1 non-cached operations) should not be
226+ # written to the set of Bash parameters.
227+ if param_opts ['VARIANT' ] == 1 :
228+ msg = f"bash generator - `{ varname } ={ value } ` skipped because"
229+ msg += f" it is a non-cached (type-1) operation."
230+ msg += f" To generate a bash arg for this consider adding FORCE or a TYPE."
231+ self .exception_control_event ("WARNING" , ValueError , message = msg )
232+ return None
233+
234+ # If varname has already been assigned and this assignment
235+ # does not include FORCE then we should skip adding it to the
236+ # set of command line options.
237+ if varname in self ._var_formatter_cache and not param_opts ['FORCE' ]:
238+ msg = f"bash generator - `{ varname } ={ value } ` skipped because"
239+ msg += f" CACHE var `{ varname } ` is already set and CMake requires"
240+ msg += f" FORCE to be set to change the value."
241+ self .exception_control_event ("WARNING" , ValueError , message = msg )
242+ return None
243+
244+ # Prepend `-D` to the parameters
204245 params = ["-D" , varname ]
205246
206- if param_opts ['VARIANT' ] == 1 :
207- # if PARENT_SCOPE was given to something that is typed and forced us to
208- # be a type-1 variant, then we assign the list "<value>;CACHE;<type>;<docstring>"
209- if param_opts ['TYPE' ] != None :
210- value += f";CACHE;{ param_opts ['TYPE' ]} ;"
211-
212- if param_opts ['VARIANT' ] == 2 and param_opts ['TYPE' ] is not None :
213- params .append (":" + param_opts ['TYPE' ])
214-
215- # Cache 'known' CMake vars here.
216- try :
217- if self ._var_formatter_cache [varname ] is not None and not param_opts ['FORCE' ]:
218- # Do not add this to the output unless it's a forceful set
219- return None
220- except :
221- pass
247+ # If the type is provided then include the `:<typename>` argument.
248+ # Note: CMake defaults to STRING if not provided.
249+ #if param_opts['VARIANT'] == 2 and param_opts['TYPE'] is not None: # DEADCODE: this can't be false now
250+ params .append (":" + param_opts ['TYPE' ])
222251
252+ # Save variable to the cache of 'known'/'set' cmake variables
223253 self ._var_formatter_cache [varname ] = value
224254
225255 return self ._generic_program_option_handler_bash (params , value )
0 commit comments