Skip to content

Commit ddb362c

Browse files
jcfrvyasr
andcommitted
wip(UseCython): Deprecate PY2 and PY3 options and introduce LANGUAGE_LEVEL
Co-authored-by: Vyas Ramasubramani <[email protected]>
1 parent e01177e commit ddb362c

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/cython_cmake/cmake/UseCython.cmake

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@
4040
# generated, unless the C language is not enabled for the project; in this
4141
# case, a C++ file is generated by default.
4242
#
43-
# ``PY2 | PY3``
43+
# ``LANGUAGE_LEVEL [2 | 3 | 3str]``
44+
# Force compilation using either Python-2, Python-3 or Python-3str syntax and
45+
# code semantics.
46+
# By default, Python-3str syntax and semantics are used if the major version
47+
# of Python found is 3 and Cython >=3 is used. Otherwise, Python-2 syntax and
48+
# semantics are used.
49+
# Using the Python-3str syntax and semantics means there cython does not assume
50+
# unicode is used by by default for string literals under Python 2
51+
#
52+
# ``PY2 | PY3`` (deprecated)
4453
# Force compilation using either Python-2 or Python-3 syntax and code
4554
# semantics. By default, Python-2 syntax and semantics are used if the major
4655
# version of Python found is 2. Otherwise, Python-3 syntax and semantics are
@@ -103,8 +112,8 @@ set(CYTHON_FLAGS "" CACHE STRING
103112
mark_as_advanced(CYTHON_ANNOTATE CYTHON_FLAGS)
104113

105114
function(add_cython_target _name)
106-
seT(_deprecated_options C CXX)
107-
set(_options PY2 PY3)
115+
set(_deprecated_options C CXX PY2 PY3)
116+
set(_options )
108117
set(_one_value TARGET_LANGUAGE OUTPUT_VAR)
109118
set(_multi_value )
110119

@@ -125,6 +134,16 @@ function(add_cython_target _name)
125134
endif()
126135
endif()
127136

137+
# Support deprecated options: PY2, PY3
138+
if(NOT _args_LANGUAGE_LEVEL)
139+
if(_args_PY2)
140+
set(_args_LANGUAGE_LEVEL "2")
141+
endif()
142+
if(_args_PY3)
143+
set(_args_LANGUAGE_LEVEL "3")
144+
endif()
145+
endif()
146+
128147
list(GET _args_UNPARSED_ARGUMENTS 0 _arg0)
129148

130149
# if provided, use _arg0 as the input file path
@@ -169,24 +188,21 @@ function(add_cython_target _name)
169188
set(_target_language_arg ${_target_language_${_target_language}_arg})
170189
set(_target_language_extension ${_target_language_${_target_language}_extension})
171190

172-
# Doesn't select an input syntax - Cython
173-
# defaults to 2 for Cython 2 and 3 for Cython 3
174-
set(_input_syntax "default")
175-
176-
if(_args_PY2)
177-
set(_input_syntax "PY2")
191+
# Set language level
192+
set(_language_level ${_args_LANGUAGE_LEVEL})
193+
if(NOT _args_LANGUAGE_LEVEL)
194+
set(_language_level "3str")
195+
else()
196+
if(NOT _language_level MATCHES "^(2|3|3str)$")
197+
message(FATAL_ERROR "LANGUAGE_LEVEL must be one of 2, 3, or 3str")
198+
endif()
178199
endif()
179200

180-
if(_args_PY3)
181-
set(_input_syntax "PY3")
182-
endif()
201+
set(_language_level_2_arg "-2")
202+
set(_language_level_3_arg "-3")
203+
set(_language_level_3str_arg "--3str")
183204

184-
set(language_level_arg "")
185-
if(_input_syntax STREQUAL "PY2")
186-
set(language_level_arg "-2")
187-
elseif(_input_syntax STREQUAL "PY3")
188-
set(language_level_arg "-3")
189-
endif()
205+
set(_language_level_arg "${_language_level_${_language_level}_arg}")
190206

191207
set(generated_file "${CMAKE_CURRENT_BINARY_DIR}/${_name}.${_target_language_extension}")
192208
set_source_files_properties(${generated_file} PROPERTIES GENERATED TRUE)
@@ -366,7 +382,7 @@ function(add_cython_target _name)
366382
ARGS
367383
${_target_language_arg}
368384
${include_directory_arg}
369-
${language_level_arg}
385+
${_language_level_arg}
370386
${annotate_arg}
371387
${cython_debug_arg}
372388
${line_directives_arg}

0 commit comments

Comments
 (0)