Skip to content

Commit ed758d8

Browse files
committed
Change cefpython API header file name depending on Python version (cztomczak#315)
1 parent 7e4d587 commit ed758d8

File tree

5 files changed

+72
-47
lines changed

5 files changed

+72
-47
lines changed

src/common/cefpython_public_api.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define CEFPYTHON_PUBLIC_API_H
1111

1212
#if defined(OS_WIN)
13-
#pragma warning(disable:4190) // cefpython_fixed.h extern C-linkage warnings
13+
#pragma warning(disable:4190) // cefpython API extern C-linkage warnings
1414
#endif
1515

1616
#include "Python.h"
@@ -30,6 +30,18 @@
3030
#include "include/cef_command_line.h"
3131
#include "util.h"
3232

33-
#include "../../build/build_cefpython/cefpython_fixed.h"
33+
#if PY_MAJOR_VERSION == 2
34+
#if PY_MINOR_VERSION == 7
35+
#include "../../build/build_cefpython/cefpython_py27_fixed.h"
36+
#endif // PY_MINOR_VERSION
37+
#elif PY_MAJOR_VERSION == 3
38+
#if PY_MINOR_VERSION == 4
39+
#include "../../build/build_cefpython/cefpython_py34_fixed.h"
40+
#elif PY_MINOR_VERSION == 5
41+
#include "../../build/build_cefpython/cefpython_py35_fixed.h"
42+
#elif PY_MINOR_VERSION == 6
43+
#include "../../build/build_cefpython/cefpython_py36_fixed.h"
44+
#endif // PY_MINOR_VERSION
45+
#endif // PY_MAJOR_VERSION
3446

3547
#endif // CEFPYTHON_PUBLIC_API_H

tools/build.py

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,23 @@ def main():
8282
check_cython_version()
8383
check_directories()
8484
setup_environ()
85-
if os.path.exists(CEFPYTHON_H):
86-
fix_cefpython_h()
85+
if os.path.exists(CEFPYTHON_API_HFILE):
86+
fix_cefpython_api_header_file()
8787
if WINDOWS:
8888
compile_cpp_projects_with_setuptools()
8989
elif MAC or LINUX:
9090
compile_cpp_projects_unix()
9191
else:
92-
print("[build.py] INFO: Looks like first run, as cefpython.h"
93-
" is missing. Skip building C++ projects.")
92+
print("[build.py] INFO: Looks like first run, as"
93+
" cefpython_py{pyver}.h is missing. Skip building"
94+
" C++ projects."
95+
.format(pyver=PYVERSION))
9496
global FIRST_RUN
9597
FIRST_RUN = True
9698
clear_cache()
9799
copy_and_fix_pyx_files()
98100
build_cefpython_module()
99-
fix_cefpython_h()
101+
fix_cefpython_api_header_file()
100102
install_and_run()
101103

102104

@@ -282,49 +284,52 @@ def check_directories():
282284
assert os.path.exists(CEFPYTHON_BINARY)
283285

284286

285-
def fix_cefpython_h():
286-
"""This function does two things: 1) Disable warnings in cefpython.h
287-
and 2) Make a copy named cefpython_fixed.h - this copy will be used
288-
by C++ projects and its modification time won't change every time
289-
you run build.py script, thus C++ won't rebuild each time."""
287+
def fix_cefpython_api_header_file():
288+
"""This function does two things: 1) Disable warnings in cefpython
289+
API header file and 2) Make a copy named cefpython_pyXX_fixed.h,
290+
this copy will be used by C++ projects and its modification time
291+
won't change every time you run build.py script, thus C++ won't
292+
rebuild each time."""
290293

291-
# Fix cefpython.h to disable this warning:
294+
# Fix cefpython_pyXX.h to disable this warning:
292295
# > warning: 'somefunc' has C-linkage specified, but returns
293296
# > user-defined type 'sometype' which is incompatible with C
294297
# On Mac this warning must be disabled using -Wno-return-type-c-linkage
295298
# flag in makefiles.
296299

297-
print("[build.py] Fix cefpython.h in the build_cefpython/ directory")
298-
if not os.path.exists(CEFPYTHON_H):
299-
assert not os.path.exists(CEFPYTHON_H_FIXED)
300-
print("[build.py] cefpython.h was not yet generated")
300+
print("[build.py] Fix cefpython API header file in the build_cefpython/"
301+
" directory")
302+
if not os.path.exists(CEFPYTHON_API_HFILE):
303+
assert not os.path.exists(CEFPYTHON_API_HFILE_FIXED)
304+
print("[build.py] cefpython API header file was not yet generated")
301305
return
302306

303-
with open(CEFPYTHON_H, "rb") as fo:
307+
with open(CEFPYTHON_API_HFILE, "rb") as fo:
304308
contents = fo.read().decode("utf-8")
305309

306310
already_fixed = False
307311
pragma = "#pragma warning(disable:4190)"
308312
if pragma in contents:
309313
already_fixed = True
310-
print("[build.py] cefpython.h is already fixed")
314+
print("[build.py] cefpython API header file is already fixed")
311315
else:
312316
if not MAC:
313317
contents = ("%s\n\n" % pragma) + contents
314318

315319
if not already_fixed:
316-
with open(CEFPYTHON_H, "wb") as fo:
320+
with open(CEFPYTHON_API_HFILE, "wb") as fo:
317321
fo.write(contents.encode("utf-8"))
318-
print("[build.py] Save cefpython.h")
322+
print("[build.py] Save {filename}"
323+
.format(filename=CEFPYTHON_API_HFILE))
319324

320-
if os.path.exists(CEFPYTHON_H_FIXED):
321-
with open(CEFPYTHON_H_FIXED, "rb") as fo:
325+
if os.path.exists(CEFPYTHON_API_HFILE_FIXED):
326+
with open(CEFPYTHON_API_HFILE_FIXED, "rb") as fo:
322327
contents_fixed = fo.read().decode("utf-8")
323328
else:
324329
contents_fixed = ""
325330
if contents != contents_fixed:
326331
print("[build.py] Save cefpython_fixed.h")
327-
with open(CEFPYTHON_H_FIXED, "wb") as fo:
332+
with open(CEFPYTHON_API_HFILE_FIXED, "wb") as fo:
328333
fo.write(contents.encode("utf-8"))
329334

330335

@@ -437,9 +442,9 @@ def compile_cpp_projects_unix():
437442

438443
# Need to allow continuing even when make fails, as it may
439444
# fail because the "public" function declaration is not yet
440-
# in "cefpython.h", but for it to be generated we need to run
441-
# cython compiling, so in this case you continue even when make
442-
# fails and then run the compile.py script again and this time
445+
# in cefpython API header file, but for it to be generated we need
446+
# to run cython compiling, so in this case you continue even when
447+
# make fails and then run the compile.py script again and this time
443448
# make should succeed.
444449

445450
# -- CLIENT_HANDLER
@@ -533,15 +538,16 @@ def copy_and_fix_pyx_files():
533538

534539
os.chdir(BUILD_CEFPYTHON)
535540
print("\n")
536-
mainfile = "cefpython.pyx"
541+
mainfile_original = "cefpython.pyx"
542+
mainfile_newname = "cefpython_py{pyver}.pyx".format(pyver=PYVERSION)
537543

538544
pyxfiles = glob.glob("../../src/*.pyx")
539545
if not len(pyxfiles):
540546
print("[build.py] ERROR: no .pyx files found in root")
541547
sys.exit(1)
542-
pyxfiles = [f for f in pyxfiles if f.find(mainfile) == -1]
543-
# Now, pyxfiles contains all pyx files except the mainfile (cefpython.pyx),
544-
# we do not fix includes in mainfile.
548+
pyxfiles = [f for f in pyxfiles if f.find(mainfile_original) == -1]
549+
# Now, pyxfiles contains all pyx files except mainfile_original
550+
# (cefpython.pyx), we do not fix includes in mainfile.
545551

546552
pyxfiles2 = glob.glob("../../src/handlers/*.pyx")
547553
if not len(pyxfiles2):
@@ -562,19 +568,19 @@ def copy_and_fix_pyx_files():
562568

563569
# Copy cefpython.pyx and fix includes in cefpython.pyx, eg.:
564570
# include "handlers/focus_handler.pyx" becomes include "focus_handler.pyx"
565-
shutil.copy("../../src/%s" % mainfile, "./%s" % mainfile)
566-
with open("./%s" % mainfile, "rb") as fo:
571+
shutil.copy("../../src/%s" % mainfile_original, "./%s" % mainfile_newname)
572+
with open("./%s" % mainfile_newname, "rb") as fo:
567573
content = fo.read().decode("utf-8")
568574
(content, subs) = re.subn(r"^include \"handlers/",
569575
"include \"",
570576
content,
571577
flags=re.MULTILINE)
572578
# Add __version__ variable in cefpython.pyx
573-
print("[build.py] Add __version__ variable to %s" % mainfile)
579+
print("[build.py] Add __version__ variable to %s" % mainfile_newname)
574580
content = ('__version__ = "{}"\n'.format(VERSION)) + content
575-
with open("./%s" % mainfile, "wb") as fo:
581+
with open("./%s" % mainfile_newname, "wb") as fo:
576582
fo.write(content.encode("utf-8"))
577-
print("[build.py] Fix %s includes in %s" % (subs, mainfile))
583+
print("[build.py] Fix %s includes in %s" % (subs, mainfile_newname))
578584

579585
# Copy the rest of the files
580586
print("[build.py] Fix includes in other .pyx files")
@@ -682,10 +688,12 @@ def build_cefpython_module():
682688

683689
# Check if built succeeded after pyx files were removed
684690
if ret != 0:
685-
if FIRST_RUN and os.path.exists(CEFPYTHON_H):
691+
if FIRST_RUN and os.path.exists(CEFPYTHON_API_HFILE):
686692
print("[build.py] INFO: looks like this was first run and"
687-
" linking is expected to fail in such case. Will re-run"
688-
" the build.py script programmatically now.")
693+
" building the cefpython module is expected to fail"
694+
" in such case due to cefpython API header file not"
695+
" being generated yet. Will re-run the build.py script"
696+
" programmatically now.")
689697
args = list()
690698
args.append(sys.executable)
691699
args.append(os.path.join(TOOLS_DIR, os.path.basename(__file__)))

tools/build_cpp_projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def smart_compile(compiler, macros, extra_args, sources, output_dir):
221221
obj_time = os.path.getmtime(obj_file)
222222
source_time = os.path.getmtime(source_file)
223223
header_time = os.path.getmtime(header_file) if header_file else 0
224-
cefpython_h_fixed_time = os.path.getmtime(CEFPYTHON_H_FIXED)
224+
cefpython_h_fixed_time = os.path.getmtime(CEFPYTHON_API_HFILE_FIXED)
225225
common_files_time = get_directory_mtime(os.path.join(SRC_DIR,
226226
"common"))
227227
changed = ((source_time > obj_time)

tools/common.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@
120120
UNITTESTS_DIR = os.path.abspath(os.path.join(ROOT_DIR, "unittests"))
121121
# ----------------------------------------------------------------------------
122122

123-
# cefpython.h and fixed version
124-
CEFPYTHON_H = os.path.join(BUILD_CEFPYTHON, "cefpython.h")
125-
CEFPYTHON_H_FIXED = os.path.join(BUILD_CEFPYTHON, "cefpython_fixed.h")
123+
# cefpython API header file and a fixed copy of it
124+
CEFPYTHON_API_HFILE = os.path.join(BUILD_CEFPYTHON,
125+
"cefpython_py{pyver}.h"
126+
.format(pyver=PYVERSION))
127+
CEFPYTHON_API_HFILE_FIXED = os.path.join(BUILD_CEFPYTHON,
128+
"cefpython_py{pyver}_fixed.h"
129+
.format(pyver=PYVERSION))
126130

127131
# Result libraries paths
128132
CEFPYTHON_APP_LIB = os.path.join(BUILD_CEFPYTHON_APP,

tools/cython_setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
ModuleNode.generate_extern_c_macro_definition)
5050

5151
def generate_extern_c_macro_definition(self, code):
52-
# This code is written to both cefpython.h and cefpython.cpp
52+
# This code is written by Cython to both cefpython API header file
53+
# and cefpython module cpp file.
5354
g_generate_extern_c_macro_definition_old(self, code)
5455
code.putln("// Added by: cefpython/tools/cython_setup.py")
5556
code.putln("#undef PyMODINIT_FUNC")
@@ -97,8 +98,8 @@ def main():
9798

9899
print("[cython_setup.py] Execute setup()")
99100
setup(
100-
name='cefpython_py%s' % PYVERSION,
101-
cmdclass={'build_ext': build_ext},
101+
name="cefpython_py{pyver}".format(pyver=PYVERSION),
102+
cmdclass={"build_ext": build_ext},
102103
ext_modules=get_ext_modules(options)
103104
)
104105

@@ -370,7 +371,7 @@ def get_libraries():
370371
def get_ext_modules(options):
371372
ext_modules = [Extension(
372373
name=MODULE_NAME_NOEXT,
373-
sources=["cefpython.pyx"],
374+
sources=["cefpython_py{pyver}.pyx".format(pyver=PYVERSION)],
374375

375376
# Ignore the warning in the console:
376377
# > C:\Python27\lib\distutils\extension.py:133: UserWarning:

0 commit comments

Comments
 (0)