FR: Add project name to CLion target

I was trying to create a single CMakeLists.txt files that combines all my other projects using “add_subdirectory()” and adding all the different folders containing the plugins CLion exporters.

This is very useful for automated builds, but also just as a very convenient workflow for CLion.

Doing that works with one plugin, but fails when you have more than one, since the target name generated by the Projucer (for example “VST3”) cannot be duplicated.

To solve this, I managed to change getTargetVarName() in jucer_ProjectExport_CLion.h to also include the plugin name.

static String getTargetVarName (ProjectType::Target& target)
{
    auto targetName = String (target.getName());
    auto fixedTargetName = targetName.toUpperCase().replaceCharacter (L' ', L'_');
    auto projectName =  target.getProject()->getProjectNameString();
    auto varName =  projectName + "_" + fixedTargetName;

    return varName;
}

This required making sure the target is passed the Project object by the correct parent exporter (XCode/etc).

After that fix, I’m able to have a CMakeLists.txt containing all my projects and build all targets with one click/contextually switch editing to different plugins.

Thanks!
Eyal

1 Like

I also forgot to mention, the fix also required replacing all hard coded instances of “SHARED_CODE” in the CLion exporter with a similar function appending the project name to it.

(Probably a good idea to have a central place providing that target name anyway, I think, rather than hard coding it wherever it’s used).

Edit:
For the plugin post build copy step to work when the project is included in a top project like that, also replace {CMAKE_BINARY_DIR} with {CMAKE_CURRENT_BINARY_DIR} which would get it’s value from the “parent” CMake file, if that one exists, and will work exactly the same otherwise.

Hey @ed95, @t0m:

I managed to simplify the needed changes to the CLion exporter.

All it does is remove all the places where the target name (such as “Shared_Code”, “VST3”, etc was hard coded, and instead call functions that append the project name to the target.

Is it possible to add that change to the repo?
It really simplifies creating build scripts and combined projects using plain CMake or CLion.

Thanks!

jucer_ProjectExport_CLion.h (53.4 KB)

1 Like

CMake target names must match the regex [A-Za-z0-9_.+-]+, so you can’t use projectName directly without pre-processing it.

This is how Reprojucer.cmake (from FRUT) does it:

1 Like

You’re totally right, but can you imagine a project that won’t use these characters? I imagine using these invalid characters would cause problems even in non cmake builds, right?

I guess removing spaces makes sense though.

@McMartin I’ve added this fix, does it make sense to you?

    String getCorrectedProjectName() const
    {
        auto regex = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.+-^";
        return projectName.retainCharacters(regex);
    }

    String adjustTargetName(const String& targetName) const
    {
        return getCorrectedProjectName() + "_" + targetName;
    }

jucer_ProjectExport_CLion.h (53.7 KB)

You don’t want the ^ character retained, in the example line from FRUT @McMartin posted, the ^ in the regex means "anything that isn’t A-Z a-z 0-9 _ . + or -", those matches are replaced with _.
The example regex given for allowable characters says "1 or more characters that are either A-Z a-z 0-9 _ . + or -", see how the only difference is the ^ (this is negation in that context). It’s well worth learning regex, it can be incredibly powerful, and you can use it from C++11 onwards, which includes std::regex_replace that could be used in the same way as the example line from FRUT.

Personally I’d not define your list of allowable characters auto regex = "....", as it’s not actually a regex, call it something like auto allowableCharacters = "...." to be more explicit of its purpose.

1 Like

Thank you, @richie! You can see I’ve never dealt with Regex before…

So, is

auto allowableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.+-";

correct?

Yep. https://regexr.com/4rqur

Get in a time machine, go back 8 days and make one of your New Year Resolutions to learn Regex, there are a lot of resources on the 'net to do so. You’ll not regret it, even if it’s just the basics (which are pretty easy to grasp). :smiley:

1 Like