Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
f25fb259
Commit
f25fb259
authored
Jun 11, 2018
by
scoder
Committed by
GitHub
Jun 11, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2315 from kif/2314_compile_time_env
Compile time env available from cython command line options
parents
5f2c54b1
aace689f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
139 additions
and
37 deletions
+139
-37
Cython/Build/Cythonize.py
Cython/Build/Cythonize.py
+26
-7
Cython/Compiler/CmdLine.py
Cython/Compiler/CmdLine.py
+13
-1
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+100
-29
No files found.
Cython/Build/Cythonize.py
View file @
f25fb259
...
@@ -25,9 +25,14 @@ class _FakePool(object):
...
@@ -25,9 +25,14 @@ class _FakePool(object):
for
_
in
imap
(
func
,
args
):
for
_
in
imap
(
func
,
args
):
pass
pass
def
close
(
self
):
pass
def
close
(
self
):
def
terminate
(
self
):
pass
pass
def
join
(
self
):
pass
def
terminate
(
self
):
pass
def
join
(
self
):
pass
def
parse_directives
(
option
,
name
,
value
,
parser
):
def
parse_directives
(
option
,
name
,
value
,
parser
):
...
@@ -52,6 +57,13 @@ def parse_options(option, name, value, parser):
...
@@ -52,6 +57,13 @@ def parse_options(option, name, value, parser):
setattr
(
parser
.
values
,
dest
,
options
)
setattr
(
parser
.
values
,
dest
,
options
)
def
parse_compile_time_env
(
option
,
name
,
value
,
parser
):
dest
=
option
.
dest
old_env
=
dict
(
getattr
(
parser
.
values
,
dest
,
{}))
directives
=
Options
.
parse_compile_time_env
(
value
,
current_settings
=
old_env
)
setattr
(
parser
.
values
,
dest
,
directives
)
def
find_package_base
(
path
):
def
find_package_base
(
path
):
base_dir
,
package_path
=
os
.
path
.
split
(
path
)
base_dir
,
package_path
=
os
.
path
.
split
(
path
)
while
os
.
path
.
isfile
(
os
.
path
.
join
(
base_dir
,
'__init__.py'
)):
while
os
.
path
.
isfile
(
os
.
path
.
join
(
base_dir
,
'__init__.py'
)):
...
@@ -85,6 +97,7 @@ def cython_compile(path_pattern, options):
...
@@ -85,6 +97,7 @@ def cython_compile(path_pattern, options):
exclude_failures
=
options
.
keep_going
,
exclude_failures
=
options
.
keep_going
,
exclude
=
options
.
excludes
,
exclude
=
options
.
excludes
,
compiler_directives
=
options
.
directives
,
compiler_directives
=
options
.
directives
,
compile_time_env
=
options
.
compile_time_env
,
force
=
options
.
force
,
force
=
options
.
force
,
quiet
=
options
.
quiet
,
quiet
=
options
.
quiet
,
**
options
.
options
)
**
options
.
options
)
...
@@ -136,11 +149,17 @@ def parse_args(args):
...
@@ -136,11 +149,17 @@ def parse_args(args):
from
optparse
import
OptionParser
from
optparse
import
OptionParser
parser
=
OptionParser
(
usage
=
'%prog [options] [sources and packages]+'
)
parser
=
OptionParser
(
usage
=
'%prog [options] [sources and packages]+'
)
parser
.
add_option
(
'-X'
,
'--directive'
,
metavar
=
'NAME=VALUE,...'
,
dest
=
'directives'
,
parser
.
add_option
(
'-X'
,
'--directive'
,
metavar
=
'NAME=VALUE,...'
,
type
=
str
,
action
=
'callback'
,
callback
=
parse_directives
,
default
=
{},
dest
=
'directives'
,
default
=
{},
type
=
"str"
,
action
=
'callback'
,
callback
=
parse_directives
,
help
=
'set a compiler directive'
)
help
=
'set a compiler directive'
)
parser
.
add_option
(
'-s'
,
'--option'
,
metavar
=
'NAME=VALUE'
,
dest
=
'options'
,
parser
.
add_option
(
'-E'
,
'--compile-time-env'
,
metavar
=
'NAME=VALUE,...'
,
type
=
str
,
action
=
'callback'
,
callback
=
parse_options
,
default
=
{},
dest
=
'compile_time_env'
,
default
=
{},
type
=
"str"
,
action
=
'callback'
,
callback
=
parse_compile_time_env
,
help
=
'set a compile time environment variable'
)
parser
.
add_option
(
'-s'
,
'--option'
,
metavar
=
'NAME=VALUE'
,
dest
=
'options'
,
default
=
{},
type
=
"str"
,
action
=
'callback'
,
callback
=
parse_options
,
help
=
'set a cythonize option'
)
help
=
'set a cythonize option'
)
parser
.
add_option
(
'-3'
,
dest
=
'python3_mode'
,
action
=
'store_true'
,
parser
.
add_option
(
'-3'
,
dest
=
'python3_mode'
,
action
=
'store_true'
,
help
=
'use Python 3 syntax mode by default'
)
help
=
'use Python 3 syntax mode by default'
)
...
...
Cython/Compiler/CmdLine.py
View file @
f25fb259
...
@@ -47,10 +47,11 @@ Options:
...
@@ -47,10 +47,11 @@ Options:
--warning-errors, -Werror Make all warnings into errors
--warning-errors, -Werror Make all warnings into errors
--warning-extra, -Wextra Enable extra warnings
--warning-extra, -Wextra Enable extra warnings
-X, --directive <name>=<value>[,<name=value,...] Overrides a compiler directive
-X, --directive <name>=<value>[,<name=value,...] Overrides a compiler directive
-E, --compile-time-env name=value[,<name=value,...] Provides compile time env like DEF would do.
"""
"""
#The following experimental options are supported only on MacOSX:
#
The following experimental options are supported only on MacOSX:
# -C, --compile Compile generated .c file to .o file
# -C, --compile Compile generated .c file to .o file
# --link Link .o file to produce extension module (implies -C)
# --link Link .o file to produce extension module (implies -C)
# -+, --cplus Use C++ compiler for compiling and linking
# -+, --cplus Use C++ compiler for compiling and linking
...
@@ -174,6 +175,17 @@ def parse_command_line(args):
...
@@ -174,6 +175,17 @@ def parse_command_line(args):
except
ValueError
as
e
:
except
ValueError
as
e
:
sys
.
stderr
.
write
(
"Error in compiler directive: %s
\
n
"
%
e
.
args
[
0
])
sys
.
stderr
.
write
(
"Error in compiler directive: %s
\
n
"
%
e
.
args
[
0
])
sys
.
exit
(
1
)
sys
.
exit
(
1
)
elif
option
==
"--compile-time-env"
or
option
.
startswith
(
'-E'
):
if
option
.
startswith
(
'-E'
)
and
option
[
2
:].
strip
():
x_args
=
option
[
2
:]
else
:
x_args
=
pop_value
()
try
:
options
.
compile_time_env
=
Options
.
parse_compile_time_env
(
x_args
,
current_settings
=
options
.
compile_time_env
)
except
ValueError
as
e
:
sys
.
stderr
.
write
(
"Error in compile-time-env: %s
\
n
"
%
e
.
args
[
0
])
sys
.
exit
(
1
)
elif
option
.
startswith
(
'--debug'
):
elif
option
.
startswith
(
'--debug'
):
option
=
option
[
2
:].
replace
(
'-'
,
'_'
)
option
=
option
[
2
:].
replace
(
'-'
,
'_'
)
from
.
import
DebugFlags
from
.
import
DebugFlags
...
...
Cython/Compiler/Options.py
View file @
f25fb259
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
from
__future__
import
absolute_import
from
__future__
import
absolute_import
class
ShouldBeFromDirective
(
object
):
class
ShouldBeFromDirective
(
object
):
known_directives
=
[]
known_directives
=
[]
...
@@ -423,7 +424,7 @@ def parse_directive_list(s, relaxed_bool=False, ignore_unknown=False,
...
@@ -423,7 +424,7 @@ def parse_directive_list(s, relaxed_bool=False, ignore_unknown=False,
item
=
item
.
strip
()
item
=
item
.
strip
()
if
not
item
:
if
not
item
:
continue
continue
if
not
'='
in
item
:
if
'='
not
in
item
:
raise
ValueError
(
'Expected "=" in option "%s"'
%
item
)
raise
ValueError
(
'Expected "=" in option "%s"'
%
item
)
name
,
value
=
[
s
.
strip
()
for
s
in
item
.
strip
().
split
(
'='
,
1
)]
name
,
value
=
[
s
.
strip
()
for
s
in
item
.
strip
().
split
(
'='
,
1
)]
if
name
not
in
_directive_defaults
:
if
name
not
in
_directive_defaults
:
...
@@ -441,3 +442,73 @@ def parse_directive_list(s, relaxed_bool=False, ignore_unknown=False,
...
@@ -441,3 +442,73 @@ def parse_directive_list(s, relaxed_bool=False, ignore_unknown=False,
parsed_value
=
parse_directive_value
(
name
,
value
,
relaxed_bool
=
relaxed_bool
)
parsed_value
=
parse_directive_value
(
name
,
value
,
relaxed_bool
=
relaxed_bool
)
result
[
name
]
=
parsed_value
result
[
name
]
=
parsed_value
return
result
return
result
def
parse_variable_value
(
value
):
"""
Parses value as an option value for the given name and returns
the interpreted value.
>>> parse_variable_value('True')
True
>>> parse_variable_value('true')
'true'
>>> parse_variable_value('us-ascii')
'us-ascii'
>>> parse_variable_value('str')
'str'
>>> parse_variable_value('123')
123
>>> parse_variable_value('1.23')
1.23
"""
if
value
==
"True"
:
return
True
elif
value
==
"False"
:
return
False
elif
value
==
"None"
:
return
None
elif
value
.
isdigit
():
return
int
(
value
)
else
:
try
:
value
=
float
(
value
)
except
Exception
:
# Not a float
pass
return
value
def
parse_compile_time_env
(
s
,
current_settings
=
None
):
"""
Parses a comma-separated list of pragma options. Whitespace
is not considered.
>>> parse_compile_time_env(' ')
{}
>>> (parse_compile_time_env('HAVE_OPENMP=True') ==
... {'HAVE_OPENMP': True})
True
>>> parse_compile_time_env(' asdf')
Traceback (most recent call last):
...
ValueError: Expected "=" in option "asdf"
>>> parse_compile_time_env('NUM_THREADS=4') == {'NUM_THREADS': 4}
True
>>> parse_compile_time_env('unknown=anything') == {'unknown': 'anything'}
True
"""
if
current_settings
is
None
:
result
=
{}
else
:
result
=
current_settings
for
item
in
s
.
split
(
','
):
item
=
item
.
strip
()
if
not
item
:
continue
if
'='
not
in
item
:
raise
ValueError
(
'Expected "=" in option "%s"'
%
item
)
name
,
value
=
[
s
.
strip
()
for
s
in
item
.
split
(
'='
,
1
)]
result
[
name
]
=
parse_variable_value
(
value
)
return
result
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment