Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to have different implementations for Linux and Windows? #4453

Closed
mahozad opened this issue Mar 8, 2024 · 6 comments
Closed

How to have different implementations for Linux and Windows? #4453

mahozad opened this issue Mar 8, 2024 · 6 comments
Labels
question Not a bug, but question or comment

Comments

@mahozad
Copy link
Contributor

mahozad commented Mar 8, 2024

How to provide an expect and separate actuals for Linux and Windows?

Because something should be implemented differently depending on the OS.

@mahozad mahozad added enhancement New feature or request submitted labels Mar 8, 2024
@ryanmitchener
Copy link
Contributor

@mahozad I would recommend reading through https://kotlinlang.org/docs/multiplatform-hierarchy.html#default-hierarchy-template. You can set up your folder structure to have some target specific code via expect/actual. For windows and linux you would need to use the mingw and linux directories

@mahozad
Copy link
Contributor Author

mahozad commented Mar 11, 2024

Are you sure? I think it's for native targets not jvm targets. Is there any compose multiplatform example?

@m-sasha
Copy link
Contributor

m-sasha commented Mar 11, 2024

There are no separate source sets for JVM on Windows/Linux/macOS; you just switch (at runtime) on the OS. You can use org.jetbrains.skiko.hostOs, or any 3rd-party OS-detecting library in Java.

@ryanmitchener
Copy link
Contributor

@mahozad Sorry, I didn't realize you were trying to do this on the JVM. In that case what m-sasha said is true. There are no separate source sets for JVM targets. Instead, use the org.jetbrains.skiko.hostOs and either perform the OS check in-line or use it in your DI to inject the appropriate implementation based on OS.

@igordmn igordmn added question Not a bug, but question or comment and removed submitted enhancement New feature or request labels Mar 12, 2024
@igordmn
Copy link
Collaborator

igordmn commented Mar 12, 2024

Yes, checking it in runtime is the default way.

If you need different binaries with different code, you can separate your project by different source sets, and include different sources depending on hostOs inside build.gradle.kts in build-time.

If your project is kotlin("multiplatform"), you can define an additional sourceset this way:

kotlin {
  sourcSets {
    if (...) { // check if it is Windows
        val windowsDesktopMain by creating {
            dependsOn(desktopMain)
        }
    }
    ...
  }
  ...

@igordmn igordmn closed this as completed Mar 12, 2024
@mahozad
Copy link
Contributor Author

mahozad commented Mar 17, 2024

The above solution by igordmn does not work even though I provided actual declaration in windowsDektopMain source set (with and without applyDefaultHierarchyTemplate() ):

Expected property 'myVariable' has no actual declaration in module <MyApp> for JVM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a bug, but question or comment
Projects
None yet
Development

No branches or pull requests

4 participants