vscode-cmake-tools icon indicating copy to clipboard operation
vscode-cmake-tools copied to clipboard

Proposal: Some workflow improvements

Open ytimenkov opened this issue 9 years ago • 21 comments

I think that one of best thing with this extension is that it facilitates very low entry barrier compared CMake's configure->generate->build approach.

If it's not clear the goal is to make CMake Tools extension more robust and behave more expected.

Limitations of current implementation, especially with 'Server Mode':

(not in any particular order)

  1. Changing variant actually requires clean configure, because it assumes different compiler and CMake forbids this. Now it may be just silently ignored, depending on generator and compiler used.
  2. Same for generator: it is either fails to start/configure or will be silently ignored.
  3. Opening a configured is very different form opening an unconfigured one: a very different sets of operations available.
  4. Changing Binary dir in any way (by changing settings or any of variables being substituted) may require clean configuration and now mostly silently ignored by the extension.
  5. CMake Server should be restarted in some of cases above, e.g. when having multiple binary directories with differently configured projects.

The proposal

Introduce clear separation between 'configured' and 'unconfigured' projects.

This is both in API and UI level, for example:

  1. Unconfigured: build & debug targets selectors should not be visible, only "Configure" is possible.
  2. Configured: should be possible to build and debug, but changing variant shouldn't be allowed.

Binary directory should be sufficient to activate project

CMake Tools should "activate" backend with binary directory, which then starts providing metadata. This means that CMake should store some extra data in binary dir, like environment and variant (likely move .cmaketools.json there).

When changing binary dir CMake Tools should reinitialize all metadata.

It should be also possible to work with externally-configured project.

Introduce simple/advance mode

If opening unconfigured project CMake Tools should ask something like "Hey, it looks like this project is CMake-based. Would you like me to initialize build system for you? (Yes/No)" and then experience should be not different from the current one. This is "simple mode".

In "advanced mode" CMake Tools just keeps registry of known build directories, allowing to switch between them or initialize new. In the latter case UX could be similar to CMake-GUI tool.

Another option is to store registry and essential settings at the same time in json configuration. Something like variants.json or CMakeSettings.json. This is somewhat close to tasks.json.

In case of using json, once initial configuration is done, it is used only to get a user-friendly name for binary directory. As a stretch, CMake Tools may track changes to it and offer clean reconfigure when it detects changes.

Configuration selector

It is required for multi-configuration generator but otherwise full rebuild is needed, which is counter-intuitive and may be dangerous to do automatically (e.g. if project builds for 30 minutes).

On the other hand switching between Debug and Release is quite essential and deserves to be represented in UI so I suggest exposing this functionality in 'simple' mode which will rebuild the project.

Environment selector is available only when configuring project initially

Environment, once detected, should be stored 'by value', i.e. not as a moniker, but rather as all extra variables. This pursues multiple goals.

  1. As mentioned early, changing environment is a significant change and requires clean configure.
  2. It is very unlikely that detecting environment will return different results. Even if it does, CMake may reject or ignore new values.
  3. Detection takes time (few seconds) and avoiding this every time makes startup faster.
  4. Multiple environments are cumbersome and should not be allowed: if I activate MinGW and VS, which compiler will be used with Ninja generator?

Please up/down vote if this makes sense or leave your comments.

If this sounds useful I would like to implement this.

ytimenkov avatar Jun 21 '17 13:06 ytimenkov

+1

s-martin avatar Jun 21 '17 15:06 s-martin

In my opinion, this project should be refactored before you(or us) implement this. this source codes... are such as spaghetti, it is pretty hard something to add, or implement more. so, refactor functions, methods, implement more clearly is needed.

ArtBlnd avatar Jun 22 '17 01:06 ArtBlnd

I like any proposal for enhancements, but a few things to clarify:

  • Changing the variant should not require a clean configure. I want to try and emphasize a difference between "variant" and "toolset". A toolset (aka toolchain) encapsulates things like compiler and platform, and would definitely require a clean configure (or new build dir). I had plans to support some form of cmake-toolsets.json that did similar to cmake-variants.json, but I haven't yet gotten around to implementing it.
  • Changing the binary dir should already cause a reconfigure (and CMake Server restart). If it doesn't, that's a bug 😦.

There are pros/cons of moving .cmaketools.json. By keeping it with the project, certain options, such as the active environment, get kept between build directories.

Also, if the extension currently behaves differently when using an externally-configure build directory, that's a bug. I wanted to make sure that CMake Tools always worked with a build directory, whether configured using the extension or from the CLI/GUI/TUI.

👍 for caching the env detection. One thing that may change would be Windows SDK and Windows Kits path, so that's something to keep in mind.

Some aspects of your recommendation are still somewhat unclear to me, but I'd be glad to hear them further!

vector-of-bool avatar Jun 23 '17 03:06 vector-of-bool

Changing the variant should not require a clean configure. I want to try and emphasize a difference between "variant" and "toolset". A toolset (aka toolchain) encapsulates things like compiler and platform, and would definitely require a clean configure (or new build dir). I had plans to support some form of cmake-toolsets.json that did similar to cmake-variants.json, but I haven't yet gotten around to implementing it.

I see. May be 'clean configure' is exaggeration, but clean build is very likely. Here major surprise is that multi-config generators behaves differently, and switching config/variant just incrementally builds from last state. I would rather simulate this behavior and that's what I meant by 'binary dir registry'. For example, how this could look like:

"common": {
  "generator": "Ninja",
  "buildRoot": "build/${buildType}-${buildPlatform}",
},
"configurations": [
  {
    "name": "x86-release",
    "buildType": "RelWithDebInfo",
    "platform": "x86",
    "activateEnvironment": "MinGW",
  },
  {
    "name": "x64-debug",
    "buildType": "Debug",
    "platform": "x64",
    "activateEnvironment": "MinGW64",
  }
]

Thinking of your proposal of variants, I don't really see how it plays with editing cache. Maybe something like a set of predefined variables which are applied or used to initialize build dir?..

And having different configure options for different binary dirs is useful not only for Build Type and default linkage. For example, with and without tests. Another case I have in mind is cross-compiling for embedded devices: both toolset (i.e. arm vs avr) and variables (for different MCU models) could be different.

Changing the binary dir should already cause a reconfigure (and CMake Server restart). If it doesn't, that's a bug 😦.

I think it's doable even now (especially with other PR which expands variables right in config.ts in 3 lines). But doing it implicitly may lead to surprises, especially when substituted variable changes. Then question is what to do with previous binary dir? Should it be deleted or left? Should it be possible to reuse it? Won't it lead to unexpected results (e.g. when user changes back, some new flags may be assumed, but they aren't updated).

ytimenkov avatar Jun 23 '17 21:06 ytimenkov

It may be useful to look at how QtCreator does the "toolsets". It switches build directory based on the chosen kit. Switching toolsets is definitely a case where a new build directory is important.

vector-of-bool avatar Jun 24 '17 04:06 vector-of-bool

@vector-of-bool So what is your vision (briefly) on how toolsets, variants and editing cache should work together? What responsibilities do they have and when one should be used instead of another?

I'm asking because I have a feeling that they overlap much (plus there few ways to customize via VS Code settings).

ytimenkov avatar Jun 26 '17 19:06 ytimenkov

Sorry to vanish like I did. I have a habit of doing that, and I think it might be annoying.

I'd like to switch out the term toolset for toolchain since those are two distinct terms when talking to CMake. I think of toolchain as being the actual underlying build tools, but also including things like platform and ABI. For example, Windows 32-bit and Windows 64-bit are different toolchains, because they have different compilers and are not ABI compatible. Same goes for things like Android, Emscripten, iOS, whatever else.

A variant is rather a collection of build options and configurations, and may or may not be ABI compatible, depending on the compiler and platform. A basic example is Debug. This is a variant that includes and embeds debug information, as well as enabling debug assertions and disabling optimizations. Release is a separate variant. Projects can provide their own variants, which let users and developers identify commonly used sets of configuration options. This would be stored in a cmake-variants.json. It's more than just a set of CMake settings, which are a bit more fine-grained. A particular variant is just a set of CMake settings and corresponding values. For example, a Debug variant just has one setting value: CMAKE_BUILD_TYPE=Debug. Release, conversely has a different single setting value: CMAKE_BUILD_TYPE=Release. A more complex set of variants might look like this:

debug: {
  oneWordSummary$: 'Debug',
  description$: 'Emit debug information without performing optimizations',
  buildType: 'Debug',
  settings: {
    DEBUG_ASSERTIONS: true
  }
},
safe_release: {
  oneWordSummary$: 'SafeRelease',
  description$: 'Enable optimizations, but keep debug assertions',
  buildType: 'Release',
  settings: {
    DEBUG_ASSERTIONS: true
  }
},
release: {
  oneWordSummary$: 'Release',
  description$: 'Enable optimizations, omit debug info and assertions',
  buildType: 'Release',
  settings: {
    DEBUG_ASSERTIONS: false
  }
},

Variants aren't specific to a toolchain. It's up to the project to figure out how to apply a specific variant option when using the specific toolchain (ie. set the proper preprocessor definitions).

Editing the cache == editing CMake settings. Like above, editing the cache should be fully supported. This allows users to set particular build settings that may or may not be exposed as a variant.

vector-of-bool avatar Jul 15 '17 03:07 vector-of-bool

Just released 0.10.2. I think I want to do some refactoring for 0.11.0 to support toolchains and variants better. I, personally, have usually stuck with a single binary dir for all variants, and this spells a problem in that I don't see the bugs in CMake Tools' support for that workflow. A lot of users prefer to keep multiple binary directories around, and it makes it convenient to switch between toolchains and variants easily, as well as know exactly what a particular binary dir is building for, without the need to inspect the existing project configuration.

I foresee that it may be best to instantiate multiple backends per user toolchain and variant. This would make it extremely easy to switch between toolchains and variants, without needing to completely restart and reconfigure the CMake Tools instance.

vector-of-bool avatar Jul 17 '17 00:07 vector-of-bool

@vector-of-bool , FYI: I've started some refactoring of CMakeTools with following principles in mind:

  1. Testing-oriented. I believe that it's a luxury to NOT have automated testing. So I try to cover as many scenarios as possible. To make tests an asset and not burden, I plan to have some good combination of component and integration tests.
  2. Modularity and composition. Split large classes like common or backend into smaller pieces with limited responsibility and independent interface to compose in upper layers. Change some hard dependencies (e. g. backend doing diagnostics and output) into injected. This is essential for testability.
  3. Model-based approach. In the current code there are hard dependencies, say between common and status which prevents components to be tested in isolation. Model on the other hand breaks such direct dependencies and allows mocking / faking different scenarios for component testing.

ATM I plan to keep existing functionality and public API and just cover it with tests to make further improvements more reliable.

WIP is in my branch. I've started a new job so progress is very slow, but I appreciate any comments / suggestions (even if it's completely wrong and not how you see it).

ytimenkov avatar Jul 17 '17 12:07 ytimenkov

@ytimenkov, after months of stagnation, I've started a rewrite. This will be taking into account quite a few learned lessons and should implement a lot of the features I've wanted from the beginning, and work around some of the common problems and bugs I've been having. All my work is going into a feature/refactor-1.0 branch, which you can take a look at.

ATM, I'm working on improved toolchain selection which will be similar to QtCreator's idea of Kits. This will make it more reliable for people to switch between toolchains on all platforms, not just Windows. A few things I want to keep in mind with a rewrite:

  1. Async initialization everywhere, and invoking commands in the extension is async by default, awaiting on the initialization routines to finish. This means that a user won't be able to request CMake Tools to do anything before initialization has finished. Instead, their commands will await on initialization, so users won't be able to see any torn state.
  2. Use filesystem watchers to see changes in settings. When we save settings, we commit those changes to disk and the filesystem watcher is the one who updates the extension state with the new settings. This means that users' modifications to files and modifications by the extension itself won't fight.
  3. Caching toolchains (Kits) to a user-local data directory. User's will be able to modify this file to add their own kits via toolchains or environment settings. These will behave as first-class build environments in the same way as Visual Studio environments.
  4. No compiling without selecting a Kit.
  5. Event-driven state changes with strong separation of concerns. For example, the KitsManager class entirely controls and presents the user with Kit selection and emits an event when the Kit selection changes, which propagate through the rest of the extension.
  6. Better end-user documentation. I'm going to start work on dedicated documentation based on Sphinx and reStructuredText. I'll host these docs on my GitHub Pages.
  7. Stronger type checking. Added noImplicitAny, noImplicitReturns, noUnusedLocals, and noUnusedParameters to tsconfig.json.
  8. More tests, and more testable code.

Besides reaching feature parity in the rewrite, I want some additional features that I miss from other IDEs:

  1. Project structure view. This can be obtained via CMake Server. The current architecture makes adding this a little hairier than I would like. With VSCode Custom Explorers, this can be done in a fairly nice way.
  2. Better CMake code introspection and autocompletion. The CMake extension provides some of this functionality, but I think it could be much better.
  3. Higher-level project commands. Being able to invoke a command like "Add new C++ class header and implementation" that creates the files and inserts them into the corresponding CMakeLists.txt would be awesome but will require better CMake code introspection.

I want to say thanks for all the interested and -- event better -- work you've put into this project. It's been a great help. I know you've been busy (as have I, hence the recent stagnation), but if you (or anyone else happening to read this) would like to help, feel free to send PRs or just comment on my commits/issues.

vector-of-bool avatar Sep 23 '17 20:09 vector-of-bool

@vector-of-bool I've been to RL too recently :) (started a new job), so I'm sad I haven't finished work I started. After you started rewrite I don't see many reasons to continue what I was doing in my branch, but rather try (https://github.com/ytimenkov/vscode-cmake-tools/commits/feature/backend-refactoring).

However I will be happy if you can find anything useful there (used patterns, pieces of code) to incorporate in your refactoring. In particular 2 things I work there quite well:

  1. Backend (re)initialization. (I've picked up the idea from typescript extension, I guess), but chaining promises work amazingly well to handle case when you need to restart the backend while some initialization is already in progress.
  2. Tests. Many may be obvious or trivial, but I've recently found myself enjoying building system block by block while getting immediate feedback.

ytimenkov avatar Sep 24 '17 05:09 ytimenkov

@vector-of-bool I took a look into your refactoring branch. The kit approach looks nice.

I saw in the kit manager class source code is a combination of logic (Kit management, scan for kits...) with the UI control (Update Status bar text). This makes it hard to write unit tests for the kit management logic and make it harder to understand the code. A separation into UI control and buisness logic makes it easier to test the logic of this vscode extension. I think the tests will be important in the long term for maintance and bugfixing.

KoeMai avatar Sep 25 '17 12:09 KoeMai

@KoeMai, you're right. I'm still in early stages and hoping to get it as nice as I can in a reasonable amount of time. I'll be reworking that before finishing. Thanks for the consideration!

vector-of-bool avatar Sep 26 '17 00:09 vector-of-bool

@ytimenkov, do you still have any interest in collaborating? I'm looking for more collaborators since I'm pretty bad at keeping up with consistent updates.

vector-of-bool avatar Feb 26 '18 05:02 vector-of-bool

Could try. RL has settled down a bit although I won't have as much time I had last summer. Is there anything particular on your mind?

ytimenkov avatar Feb 26 '18 05:02 ytimenkov

Take a look at #315 and #274. In general, I'm just getting overloaded as a single person on this project.

vector-of-bool avatar Feb 26 '18 05:02 vector-of-bool

My rewrite was mostly about making the extension more testable.

Architecture: I (re)structured code into smaller relatively independent pieces (big abstract classes like current CMakeDriver are hard to test in isolation):

  1. Driver/backend: does only configure/generate and introspection of generated project, being an interface. Server implementation is covered by component tests.
  2. CMake wrapper Contains common calls to CMake and handles build logic with grabbing output, cancellation, etc. Also tested in isolation.
  3. Model: Contains the logical state of extensions (i.e. project, selected target, kit, variant, etc.). Exposes properties and notifications of property changes via events. I saw comment like "Updating status from kit manager makes testing harder". That is a solution.
  4. Glue. That functionality was in old wrapper class. It is responsible for creating and wiring up components. For example assigning buildDirectory property to model triggerred restarting backend, which in turn triggerred updating targets, etc. And this may happen by changing configuration, variant, etc. Automagically.

Reasonig: In my experience approaches with "scenario"-based tests or any tests covering multiple units at once without base of smaller component tests are much easier to write, easy to get true positive result but in long term harder to maintain, harder to get true negative result. That is make the test catching issues when something changes.

ytimenkov avatar Mar 07 '18 07:03 ytimenkov

yes, I like your next steps. I would help, I was first interested to get a bundle of tests which check the function of the extension. So that a refactoring can be done without breaking the extension functionality. For the following steps I thought about refactoring parts. My ideas go in the same direction like yours. In future I think most tests should only test the small single responsible components.

  1. yes sounds good.
  2. I would add an additional level for construction of cmake version dependent components like the driver for server and legacy mode (Factory?). I do not know if there are other components in the extension which are CMake version dependent. One question: Is a central cancelation useful? I think each component needs a separate one which is manages by a cancelation component or the extention entry point.
  3. Yes, im with you. Maybe we use a Model, which has for each main component an sub model. In this case not all components have to see all settings. The notification is a nice way to separate UI from the extension backend.
  4. Is this in Cmake-tools in the refactoring branch?

KoeMai avatar Mar 08 '18 06:03 KoeMai

I like the idea of CMakeSettings.json support. If the file is present, VSCode could simply override variety of the settings using values from this file. (at least, "cmake.buildDirectory" so it does not create build over and over, when my CMakeSettings.json create _build ;-))

mloskot avatar Mar 30 '18 13:03 mloskot

@ytimenkov for me it looks like many topics of this proposal have already been addressed? Could this issue be closed?

s-martin avatar Apr 07 '19 07:04 s-martin