1import escapeRegExp from 'lodash/escapeRegExp';
2
3import { StringTransform, transformString } from '../../Transforms';
4
5type DebugableStringTransform = StringTransform & {
6  debug?: boolean;
7};
8
9export function baseCmakeTransforms(
10  abiVersion: string,
11  libNames: string[]
12): DebugableStringTransform[] {
13  const renameFirstArg = (text: string) =>
14    transformString(
15      text,
16      libNames.map((lib) => ({
17        find: new RegExp(`^(\\\s*)${escapeRegExp(lib)}($|\\\s)`),
18        replaceWith: `$1${lib}_${abiVersion}$2`,
19      }))
20    );
21  const renameLibs = (text: string) =>
22    transformString(
23      text,
24      libNames.map((lib) => ({
25        find: new RegExp(`(^|\\\s)${escapeRegExp(lib)}($|\\\s)`, 'g'),
26        replaceWith: `$1${lib}_${abiVersion}$2`,
27      }))
28    );
29  return [
30    {
31      find: /(target_link_libraries\()([\s\S]*?)(\))/g,
32      replaceWith: (_, p1, p2, p3) => [p1, renameLibs(p2), p3].join(''),
33    },
34    {
35      find: /(add_library\()([\s\S]*?)(\))/g,
36      replaceWith: (_, p1, p2, p3) => [p1, renameFirstArg(p2), p3].join(''),
37    },
38    {
39      find: /(target_include_directories\()([\s\S]*?)(\))/g,
40      replaceWith: (_, p1, p2, p3) => [p1, renameFirstArg(p2), p3].join(''),
41    },
42    {
43      find: /(target_compile_options\()([\s\S]*?)(\))/g,
44      replaceWith: (_, p1, p2, p3) => [p1, renameFirstArg(p2), p3].join(''),
45    },
46    {
47      find: /(set_target_properties\()([\s\S]*?)(\))/g,
48      replaceWith: (_, p1, p2, p3) => [p1, renameFirstArg(p2), p3].join(''),
49    },
50  ];
51}
52