- 
                Notifications
    You must be signed in to change notification settings 
- Fork 728
Zephyr: fix the product mini compilation issue #4629
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
          
     Open
      
      
            lucasAbadFr
  wants to merge
  10
  commits into
  bytecodealliance:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
lucasAbadFr:fix/zephyr_product_mini
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 9 commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      45acd9f
              
                fix(zephyr): Make the simple-file sample compile by commenting nanosl…
              
              
                lucasAbadFr c8a7f00
              
                fix(zephyr): Real os_timespec definition with comments
              
              
                lucasAbadFr 3094ffb
              
                fix(zephyr): Introduce a os_nanosleep function:
              
              
                lucasAbadFr 9658fdd
              
                fix(zephyr): Add os_nanosleep impleemntation for POSIX platform
              
              
                lucasAbadFr 3b56bf1
              
                Merge branch 'bytecodealliance:main' into fix/zephyr_product_mini
              
              
                lucasAbadFr 01dff3a
              
                fix(zephyr): Provide a Zephyr naive implementation for os_nanosleep.
              
              
                lucasAbadFr 5c531b6
              
                Merge branch 'bytecodealliance:main' into fix/zephyr_product_mini
              
              
                lucasAbadFr d9b5d66
              
                fix(zephyr): Refactor the nanosleep and helpers functions
              
              
                lucasAbadFr b69ff79
              
                fix(zephyr): Use zephyr macros instead of hardcoded values
              
              
                lucasAbadFr 505eb2d
              
                Merge branch 'bytecodealliance:main' into fix/zephyr_product_mini
              
              
                lucasAbadFr File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright (C) 2024 Grenoble INP - ESISAR. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| */ | ||
| #include "platform_api_extension.h" | ||
|  | ||
| /* | ||
| * In Zephyr v3.7, there is no simple way to get a `nanosleep` implementation. | ||
| * But in the later version the Zephyr community introduced some clock APIs | ||
| * and their POSIX compatibility layer. | ||
| * | ||
| * Relevant Zephyr sources: | ||
| * - zephyr/include/zephyr/sys/clock.h | ||
| * - Zephyr/lib/os/clock.c | ||
| * POSIX layer: | ||
| * - zephyr/lib/posix/options/clock.c | ||
| * | ||
| * Instead of re-implementing the full Clock APIs, this file provides a naive | ||
| * `nanosleep` implementation based on the Zephyr thread API (`k_sleep`). | ||
| * | ||
| * Limitations: | ||
| * Maximum sleep duration is limited by UINT32_MAX or UINT64_MAX ticks | ||
| * (≈ 4,294,967,295 and 18,446,744,073,709,551,615 respectively). | ||
| * | ||
| * Example at a "slow" clock rate of 50 kHz: | ||
| * - UINT32_MAX: ~85 899s (~23 hours) | ||
| * - UINT64_MAX: ~368 934 881 474 191s (~11.7 millions years) | ||
| * Clearly, `nanosleep` should not be used for such long durations. | ||
| * | ||
| * Note: this assumes `CONFIG_POSIX_API=n` in the Zephyr application. | ||
| */ | ||
|  | ||
| static k_ticks_t timespec_to_ticks(const os_timespec *ts); | ||
| static void ticks_to_timespec(k_ticks_t ticks, os_timespec *ts); | ||
|  | ||
| __wasi_errno_t | ||
| os_nanosleep(const os_timespec *req, os_timespec *rem) | ||
| { | ||
| k_timeout_t timeout; | ||
| k_ticks_t rem_ticks; | ||
|  | ||
| if (req == NULL){ | ||
| return __WASI_EINVAL; | ||
| } | ||
|  | ||
| if (req->tv_sec < 0 || req->tv_nsec < 0 || req->tv_nsec >= NSEC_PER_SEC) { | ||
| return __WASI_EINVAL; | ||
| } | ||
|  | ||
| if (req->tv_sec == 0 && req->tv_nsec == 0) { | ||
| if (rem != NULL) { | ||
| rem->tv_sec = 0; | ||
| rem->tv_nsec = 0; | ||
| } | ||
| return __WASI_ESUCCESS; | ||
| } | ||
|  | ||
| timeout.ticks = timespec_to_ticks(req); | ||
|  | ||
| /* | ||
| * The function `int32_t k_sleep(k_timeout_t timeout)` return either: | ||
| * * 0 requested time elaspsed. | ||
| * * >0 remaining time in ms (due to k_wakeup). | ||
| */ | ||
| int32_t rc = k_sleep(timeout); | ||
| if (rem != NULL) { | ||
| if (rc > 0) { | ||
|  | ||
| #ifdef CONFIG_TIMEOUT_64BIT | ||
| rem_ticks = (k_ticks_t)((uint64_t)rc * CONFIG_SYS_CLOCK_TICKS_PER_SEC / MSEC_PER_SEC); | ||
| #else /* CONFIG_TIMEOUT_32BIT */ | ||
| uint64_t temp_ticks = (uint64_t)rc * CONFIG_SYS_CLOCK_TICKS_PER_SEC / MSEC_PER_SEC; | ||
| rem_ticks = (k_ticks_t)(temp_ticks > UINT32_MAX ? UINT32_MAX : temp_ticks); | ||
| #endif | ||
| ticks_to_timespec(rem_ticks, rem); | ||
| } else { | ||
| rem->tv_sec = 0; | ||
| rem->tv_nsec = 0; | ||
| } | ||
| } | ||
|  | ||
| return __WASI_ESUCCESS; | ||
| } | ||
|  | ||
|  | ||
| static k_ticks_t timespec_to_ticks(const os_timespec *ts) | ||
| { | ||
| const uint64_t ticks_per_sec = CONFIG_SYS_CLOCK_TICKS_PER_SEC; | ||
| uint64_t total_ns, ticks; | ||
|  | ||
| total_ns = (uint64_t)ts->tv_sec * NSEC_PER_SEC + (uint64_t)ts->tv_nsec; | ||
| ticks = total_ns * ticks_per_sec / NSEC_PER_SEC; | ||
|  | ||
| #ifdef CONFIG_TIMEOUT_64BIT | ||
| if (ticks > INT64_MAX) { | ||
| return INT64_MAX; | ||
| } | ||
| #else /* CONFIG_TIMEOUT_32BIT */ | ||
| if (ticks > UINT32_MAX) { | ||
| return UINT32_MAX; | ||
| } | ||
| #endif | ||
|  | ||
| return (k_ticks_t)ticks; | ||
| } | ||
|  | ||
| static void ticks_to_timespec(k_ticks_t ticks, os_timespec *ts) | ||
| { | ||
| const uint64_t ticks_per_sec = CONFIG_SYS_CLOCK_TICKS_PER_SEC; | ||
| uint64_t total_ns; | ||
|  | ||
| if (ts == NULL) { | ||
| return; | ||
| } | ||
|  | ||
| total_ns = ((uint64_t)ticks * NSEC_PER_SEC) / ticks_per_sec; | ||
|  | ||
| ts->tv_sec = (long)(total_ns / NSEC_PER_SEC); | ||
| ts->tv_nsec = (long)(total_ns % NSEC_PER_SEC); | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not a standard libc header and leads to a compilation error:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I know, but it didn't anticipate this error.
In fact I tested building iwasm for Linux (file sample) and no other plateforms (except Zephyr obviously).
From what I see, the issue comes from this CMake logic:
wasm-micro-runtime/core/shared/platform/common/posix/platform_api_posix.cmake
Lines 15 to 22 in 95f506a
Now that
posix_sleep.cdirectly includelibc_errno.h, we can build withWAMR_BUILD_LIBC_WASI=0but we still need this helper.How do we manage this case ? We can:
caseinos_nanosleeponretfor POSIX.