]> jfr.im git - irc/znc/znc.git/blame - znc-buildmod.cmake.in
Merge branch '1.9.x'
[irc/znc/znc.git] / znc-buildmod.cmake.in
CommitLineData
e95f0ea6 1#!/bin/sh
561a1805 2#
17a004aa 3# Copyright (C) 2004-2024 ZNC, see the NOTICE file for details.
561a1805
AS
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
e95f0ea6
AS
18# http://stackoverflow.com/questions/18993438/shebang-env-preferred-python-version
19# http://stackoverflow.com/questions/12070516/conditional-shebang-line-for-different-versions-of-python
20""":"
21which python3 >/dev/null 2>&1 && exec python3 "$0" "$@"
22which python >/dev/null 2>&1 && exec python "$0" "$@"
23which python2 >/dev/null 2>&1 && exec python2 "$0" "$@"
24echo "Error: znc-buildmod requires python"
25exec echo "Either install python, or use cmake directly"
26":"""
27
28from __future__ import print_function
561a1805
AS
29
30import argparse
31import glob
32import os
33import shutil
34import subprocess
e95f0ea6 35import sys
561a1805
AS
36import tempfile
37
e95f0ea6
AS
38if sys.version_info < (3, 0):
39 class TemporaryDirectory(object):
40 def __enter__(self):
41 self.name = tempfile.mkdtemp()
42 return self.name
43 def __exit__(self, *a, **k):
44 shutil.rmtree(self.name)
45 tempfile.TemporaryDirectory = TemporaryDirectory
46
561a1805
AS
47parser = argparse.ArgumentParser(
48 description='Build external ZNC modules and place the results to '
49 'current directory. Several modules can be built at once.',
50 epilog='Adjustable environment variables: CXXFLAGS, LDFLAGS, LIBS')
51parser.add_argument('-v', '--verbose', action='count', default=0,
52 help='use -vvv for more verbosity')
53parser.add_argument('files', nargs='+', metavar='file.cpp',
54 help="path to the module's source file")
55args = parser.parse_args()
56
57with tempfile.TemporaryDirectory() as cmdir:
58 with open(os.path.join(cmdir, 'CMakeLists.txt'), 'w') as cm:
2b9a582d 59 print('cmake_minimum_required(VERSION 3.13)', file=cm)
34645658 60 print('project(ExternalModules LANGUAGES CXX)', file=cm)
88d1e27c
AS
61 print('find_package(ZNC @ZNC_VERSION_MAJOR@.@ZNC_VERSION_MINOR@ HINTS '
62 '@CMAKE_INSTALL_FULL_DATADIR@/znc REQUIRED)', file=cm)
561a1805
AS
63 if args.verbose > 0:
64 print('set(CMAKE_VERBOSE_MAKEFILE true)', file=cm)
65
66 for mod_cpp in args.files:
67 mod, _ = os.path.splitext(os.path.basename(mod_cpp))
68 print(file=cm)
69 print('add_library(module_{} MODULE {})'.format(
70 mod, os.path.abspath(mod_cpp)), file=cm)
71 print('znc_setup_module(TARGET module_{} NAME {})'.format(mod, mod),
72 file=cm)
73 print('target_link_libraries(module_{} PRIVATE {})'.format(
74 mod, os.environ.get('LIBS', '')), file=cm)
75
76 if args.verbose > 0:
77 with open(os.path.join(cmdir, 'CMakeLists.txt')) as cm:
78 print(cm.read())
79
80 with tempfile.TemporaryDirectory() as build:
d7133125 81 command = ['cmake', cmdir]
561a1805 82 if args.verbose > 1:
a2d16817 83 command.append('--debug-output')
561a1805 84 if args.verbose > 2:
a2d16817 85 command.append('--trace')
561a1805
AS
86 if args.verbose > 0:
87 print(command)
88 subprocess.check_call(command, cwd=build)
89 subprocess.check_call(['cmake', '--build', '.'], cwd=build)
90
91 for so in glob.iglob(os.path.join(build, '*.so')):
25a88004 92 print('Writing {}'.format(os.path.join(os.getcwd(), os.path.basename(so))))
bbc43e20
AS
93 try:
94 os.remove(os.path.basename(so))
95 except OSError:
96 pass
561a1805 97 shutil.copy(so, os.getcwd())